设计模式(21):备忘录模式

核心

  • 就是保存某个对象内部状态的拷贝,这样以后就可以将对象恢复到原先的状态。

结构

  • 源发器类(Originator): 负责人需要恢复的对象,它定义了生成备忘录对象的方法,也定义了恢复上一个源发器对象的方法;
  • 备忘录类(Memento): 用于保存负责人需要恢复的对象;
  • 负责人类(CareTaker): 管理备忘录对象;

场景

  • 录入大批人员资料。正在录入当前人资料时,发现上一个人录错了,此时需要恢复上一个资料,再进行修改。
  • word文档编剧时,忽然电脑死机或断电,再打开时,可以看到word提示你恢复到以前的文档。
  • 管理系统中,公文撤回功能。公文发送出去后,想撤回来。
  • windows返回上一步(回退)、下一步(前进)功能。
  • 棋牌游戏中,悔棋
  • 普通软件中,撤销功能
  • 数据库软件中,事务管理中的回滚功能

代码实现

  • 源发器类(Originator)
java 复制代码
/**
 * 源发器对象
 */
public class Emp {
	private String name;
	private int age;
	private double salary;
	//进行备忘操作,并返回备忘对象
	public EmpMemento memento(){
		return new EmpMemento(this);	
	}
	//进行数据恢复,恢复成指定备忘录对象的值
	public void recovery(EmpMemento memneto){
		this.name=memneto.getName();
		this.age=memneto.getAge();
		this.salary=memneto.getSalary();
	}
	public Emp(String name, int age, double salary) {
		this.name = name;
		this.age = age;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public double getSalary() {
		return salary;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
}
  • 备忘录类(Memento)
java 复制代码
/**
 * 备忘录对象
 */
public class EmpMemento {
	private String name;
	private int age;
	private double salary;
	public EmpMemento(Emp e) {
		this.name = e.getName();
		this.age = e.getAge();
		this.salary =e.getSalary();
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public double getSalary() {
		return salary;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
}
  • 负责人类(CareTaker)
java 复制代码
/**
 * 负责人对象
 * 负责管理备忘录对象
 */
public class CareTaker {
	private Stack<EmpMemento> stack=new Stack(); //很多备忘录对象
	public EmpMemento getMemento() {
		return stack.pop();
	}
	public void setMemento(EmpMemento memento) {
		stack.add(memento);
	}	
}
  • 客户端调用
java 复制代码
public static void main(String[] args) {
	CareTaker t=new CareTaker();
	Emp e=new Emp("张三",18,1800);
	System.out.println("没更改:"+e.getName()+"\t"+e.getAge()+"\t"+e.getSalary());
	t.setMemento(e.memento());//备忘
	e.setName("李四");
	e.setAge(28);
	e.setSalary(2800);
	System.out.println("第一次修改后:"+e.getName()+"\t"+e.getAge()+"\t"+e.getSalary());
	t.setMemento(e.memento());//备忘
	e.setName("王五");
	e.setAge(38);
	e.setSalary(3800);
	System.out.println("第二次修改后:"+e.getName()+"\t"+e.getAge()+"\t"+e.getSalary());
	e.recovery(t.getMemento());//恢复到备忘录保存的对象
	System.out.println("第一次恢复后:"+e.getName()+"\t"+e.getAge()+"\t"+e.getSalary());
	e.recovery(t.getMemento());//恢复到备忘录保存的对象
	System.out.println("第二次恢复后:"+e.getName()+"\t"+e.getAge()+"\t"+e.getSalary());
}

更多设计模式学习:

设计模式(1):介绍

设计模式(2):单例模式

设计模式(3):工厂模式

设计模式(4):建造者模式

设计模式(5):原型模式

设计模式(6):桥接模式

设计模式(7):装饰器模式

设计模式(8):组合模式

设计模式(9):外观模式

设计模式(10):享元模式

设计模式(11):适配器模式

设计模式(12):代理模式

设计模式(13):模板方法模式

设计模式(14):命令模式

设计模式(15):迭代器模式

设计模式(16):观察者模式

设计模式(17):中介者模式

设计模式(18):状态模式

设计模式(19):策略模式

设计模式(20):责任链模式

设计模式持续更新中...

相关推荐
float_六七2 小时前
Java——单例类设计模式
java·单例模式·设计模式
老菜鸟的每一天2 小时前
创建型模式-Prototype 模式(原型模式)
设计模式·原型模式
码熔burning3 小时前
(五)趣学设计模式 之 建造者模式!
java·设计模式·建造者模式
黑不溜秋的11 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
付聪121013 小时前
策略模式介绍和代码示例
设计模式
ThereIsNoCode14 小时前
「软件设计模式」状态模式(State)
设计模式·状态模式
小猫猫猫◍˃ᵕ˂◍16 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
菜鸟一枚在这20 小时前
深入理解设计模式之代理模式
java·设计模式·代理模式
mjr1 天前
设计模式-Java
java·设计模式
yuanpan1 天前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式