备忘录模式实例-象棋“悔棋”的实现

本例需提前了解:

备忘录模式

UML图

在这里插入图片描述

象棋原发器类

public class ChessmanOriginator {
	private String name;//各种状态
	private int x;//各种状态
	private int y;//各种状态

	public ChessmanOriginator(String name, int x, int y) {
		this.name = name;
		this.x = x;
		this.y = y;
	}

	public ChessmanOriginator() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public ChessmanMemento CreateChessmanMemento() throws CloneNotSupportedException {
		return (ChessmanMemento) (new ChessmanMemento(name, x, y)).clone();
    //以原型模式中克隆的方式创造备忘录类实例并返回
	}

	public void SetChessmanMemento(ChessmanMemento memento) {//用备忘录类恢复状态
		this.name = memento.getName();
		this.x = memento.getX();
		this.y = memento.getY();
	}

	public void Show() {
		System.out.println("棋子:" + name + "坐标:" + x + "," + y);
	}

}

象棋备忘录类

public class ChessmanMemento implements Cloneable {//继承克隆接口
	private String name;
	private int x;
	private int y;

	public ChessmanMemento(String name, int x, int y) {
		this.name = name;
		this.x = x;
		this.y = y;
	}

	public ChessmanMemento() {
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return (ChessmanMemento) super.clone();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}

管理者类

public class ChessmanCaretaker {

	public ChessmanCaretaker() {
	}

	private List<ChessmanMemento> mementoList = new ArrayList<ChessmanMemento>();

	public ChessmanMemento huiQi() {// 确保每一步都被保存下来
		int index = mementoList.size();
		if (index >= 2) {//如果保存了前一状态
			ChessmanMemento c = mementoList.get(index - 2);
			mementoList.remove(index - 1);
			return c;
		} else {
			ChessmanMemento c = new ChessmanMemento("空", 0, 0);
			return c;
		}

	}

	public void setMemento(ChessmanMemento memento) {
		mementoList.add(memento);
	}

}

客户端类

public class Client {

	public static void main(String[] args) {
		ChessmanOriginator Co = new ChessmanOriginator();
		ChessmanCaretaker Cc = new ChessmanCaretaker();

		Co.setName("炮1");
		Co.setX(7);
		Co.setY(2);
		Co.Show();
		try {
			Cc.setMemento(Co.CreateChessmanMemento());//保存到备忘录
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

		Co.setName("马1");
		Co.setX(7);
		Co.setY(1);
		Co.Show();
		try {
			Cc.setMemento(Co.CreateChessmanMemento());//保存到备忘录
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

		Co.SetChessmanMemento(Cc.huiQi());// 悔棋
		Co.Show();

		Co.setName("兵1");
		Co.setX(7);
		Co.setY(1);
		Co.Show();
		try {
			Cc.setMemento(Co.CreateChessmanMemento());//保存到备忘录
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

		Co.SetChessmanMemento(Cc.huiQi());// 悔棋
		Co.Show();

	}

}

运行效果

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值