备忘录模式(java版)

1.引入

备忘录模式的核心就是再不破坏其封装性的条件下,捕获一个对象的内部状态,然后使用备忘录保存这个状态,然后就可以随便修改了,如果想要恢复其原内部状态,那么就直接用备忘录恢复原内部状态。

2.举例

在网页设计中,我们拿到别人前端设计的网页,如果我们直接就修改,那么恢复是不可能的。所以我们可以先把别人的前端网页先另保存一份在磁盘上,然后拿着原别人的前端网页开始随意修改了,如果我们想要恢复到原网页,那么直接使用磁盘的备份就可以了。而这就是备忘录模式,其实就是三步,第一步初始化,第二步备份,第三步恢复

3.实现

--3.1.原网页的样式类:

public class Original {

        //背景颜色
	private String backgroundColor;
	//字体大小
        private String fontSize;
	//字体样式
        private String fontStyle;
	
	public Original(String backgroundColor,String fontSize,String fontStyle){
		this.backgroundColor=backgroundColor;
		this.fontSize=fontSize;
		this.fontStyle=fontStyle;
	}
	public String getBackgroundColor() {
		return backgroundColor;
	}
	public void setBackgroundColor(String backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	public String getFontSize() {
		return fontSize;
	}
	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}
	public String getFontStyle() {
		return fontStyle;
	}
	public void setFontStyle(String fontStyle) {
		this.fontStyle = fontStyle;
	}
	
	public Memento createMemento(){
		return new Memento(backgroundColor,fontSize,fontStyle);
	}
	
	public void restoreMemento(Memento memento){
		this.backgroundColor=memento.getBackgroundColor();
		this.fontSize=memento.getFontSize();
		this.fontStyle=memento.getFontStyle();
	}
	
}


--3.2.备份网页样式类:

public class Memento {

	private String backgroundColor;
	private String fontSize;
	private String fontStyle;
	
	public Memento(String backgroundColor,String fontSize,String fontStyle){
		this.backgroundColor=backgroundColor;
		this.fontSize=fontSize;
		this.fontStyle=fontStyle;
	}
	public String getBackgroundColor() {
		return backgroundColor;
	}
	public void setBackgroundColor(String backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	public String getFontSize() {
		return fontSize;
	}
	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}
	public String getFontStyle() {
		return fontStyle;
	}
	public void setFontStyle(String fontStyle) {
		this.fontStyle = fontStyle;
	}
	
}

--3.3.存储备份样式类(磁盘):

public class Storage {

	private Memento memento;

	public Storage(Memento memento){
		this.memento=memento;
	}
	public Memento getMemento() {
		return memento;
	}

	public void setMemento(Memento memento) {
		this.memento = memento;
	}
	
}

--3.4.测试类:

public class Test {

	public static void main(String[] args) {
		Original original =new Original("红色","24px","宋体");
		Storage storage=new Storage(original.createMemento());
		
		System.out.println("初始化状态:");
		System.out.println(original.getBackgroundColor()+","+original.getFontSize()+","+original.getFontStyle());
		original.setBackgroundColor("黑色");
		original.setFontSize("18px");
		original.setFontStyle("微软雅黑");
		System.out.println("修改后状态:");
		System.out.println(original.getBackgroundColor()+","+original.getFontSize()+","+original.getFontStyle());
		original.restoreMemento(storage.getMemento());
		System.out.println(original.getBackgroundColor()+"-"+original.getFontSize()+"-"+original.getFontStyle());
	}
}

结果:

初始化状态:
红色,24px,宋体
修改后状态:
黑色,18px,微软雅黑
红色-24px-宋体

------------------------------------------------------------------------------------------------------------------

总结:

备忘录模式就是先初始化一个对象的状态,然后将这个状态保存成备份,当下次修改之后想要回到原状态,就用备份恢复就完事了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值