设计模式(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):责任链模式

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

相关推荐
long3161 小时前
构建者设计模式 Builder
java·后端·学习·设计模式
一乐小哥4 小时前
从 JDK 到 Spring,单例模式在源码中的实战用法
后端·设计模式
付春员7 小时前
23种设计模式
设计模式
Zyy~19 小时前
《设计模式》工厂方法模式
设计模式·工厂方法模式
ikkkkkkkl1 天前
C++设计模式:面向对象设计原则
c++·设计模式·面向对象
whitepure1 天前
万字详解Java中的面向对象(二)——设计模式
java·设计模式
稚辉君.MCA_P8_Java1 天前
豆包 Java的23种设计模式
java·linux·jvm·设计模式·kubernetes
快乐的划水a2 天前
组合模式及优化
c++·设计模式·组合模式
Zyy~2 天前
《设计模式》装饰模式
java·设计模式
落霞的思绪2 天前
Java设计模式详细解读
java·开发语言·设计模式