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

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

相关推荐
静水流深_沧海一粟20 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder20 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20105 天前
设计模式之状态模式
设计模式·状态模式
电子科技圈5 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
徐先生 @_@|||6 天前
安装依赖三方exe/msi的软件设计模式
设计模式