博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
memento模式_Java中的Memento设计模式
阅读量:2530 次
发布时间:2019-05-11

本文共 5363 字,大约阅读时间需要 17 分钟。

memento模式

Memento design pattern is one of the behavioral design pattern. Memento design pattern is used when we want to save the state of an object so that we can restore later on. Memento pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the object, this protects the integrity of saved state data.

备忘录设计模式是行为设计​​模式之一。 当我们要保存对象的状态以便以后可以恢复时,可以使用Memento设计模式。 使用Memento模式以这种方式实现该目的,即无法在对象外部访问对象的已保存状态数据,这可以保护已保存状态数据的完整性。

纪念品设计模式 (Memento Design Pattern)

Memento design pattern is implemented with two objects –
Originator and
Caretaker.

Memento设计模式由两个对象实现OriginatorCaretaker

Originator is the object whose state needs to be saved and restored and it uses an to save the state of Object. The inner class is called Memento and it’s private, so that it can’t be accessed from other objects.

发起者是需要保存和恢复其状态的对象,并且它使用来保存对象的状态。 内部类称为Memento ,它是私有的,因此不能从其他对象访问它。

Caretaker is the helper class that is responsible for storing and restoring the Originator’s state through Memento object. Since Memento is private to Originator, Caretaker can’t access it and it’s stored as an Object within the caretaker.

Caretaker是帮助程序类,负责通过Memento对象存储和还原发起者的状态。 由于Memento对发起者是私有的,因此看守者无法访问它,并且将其作为对象存储在看守者中。

备忘录设计模式Java (Memento Design Pattern Java)

One of the best real life example is the text editors where we can save it’s data anytime and use undo to restore it to previous saved state.

现实生活中最好的示例之一是文本编辑器,我们可以在其中随时保存其数据,并使用undo将其还原到以前的保存状态。

We will implement the same feature and provide a utility where we can write and save contents to a File anytime and we can restore it to last saved state. For simplicity, I will not use any IO operations to write data into file.

我们将实现相同的功能,并提供一个实用程序,使我们可以随时将内容写入并保存到文件中,并将其恢复到上次保存的状态。 为简单起见,我将不使用任何IO操作将数据写入文件。

记忆模式创建者类 (Memento Pattern Originator Class)

package com.journaldev.design.memento;public class FileWriterUtil {	private String fileName;	private StringBuilder content;		public FileWriterUtil(String file){		this.fileName=file;		this.content=new StringBuilder();	}		@Override	public String toString(){		return this.content.toString();	}		public void write(String str){		content.append(str);	}		public Memento save(){		return new Memento(this.fileName,this.content);	}		public void undoToLastSave(Object obj){		Memento memento = (Memento) obj;		this.fileName= memento.fileName;		this.content=memento.content;	}			private class Memento{		private String fileName;		private StringBuilder content;				public Memento(String file, StringBuilder content){			this.fileName=file;			//notice the deep copy so that Memento and FileWriterUtil content variables don't refer to same object			this.content=new StringBuilder(content);		}	}}

Notice the Memento inner class and implementation of save and undo methods. Now we can continue to implement Caretaker class.

注意Memento内部类以及save和undo方法的实现。 现在,我们可以继续实施看守类。

纪念品模式看守班 (Memento Pattern Caretaker Class)

package com.journaldev.design.memento;public class FileWriterCaretaker {	private Object obj;		public void save(FileWriterUtil fileWriter){		this.obj=fileWriter.save();	}		public void undo(FileWriterUtil fileWriter){		fileWriter.undoToLastSave(obj);	}}

Notice that caretaker object contains the saved state in the form of Object, so it can’t alter its data and also it has no knowledge of it’s structure.

请注意,看守对象包含对象形式的保存状态,因此它不能更改其数据,也不知道其结构。

记忆模式示例测试类 (Memento Pattern Example Test Class)

Lets write a simple test program that will use our memento pattern implementation.

让我们编写一个简单的测试程序,该程序将使用我们的memento模式实现。

package com.journaldev.design.memento;public class FileWriterClient {	public static void main(String[] args) {				FileWriterCaretaker caretaker = new FileWriterCaretaker();				FileWriterUtil fileWriter = new FileWriterUtil("data.txt");		fileWriter.write("First Set of Data\n");		System.out.println(fileWriter+"\n\n");				// lets save the file		caretaker.save(fileWriter);		//now write something else		fileWriter.write("Second Set of Data\n");				//checking file contents		System.out.println(fileWriter+"\n\n");		//lets undo to last save		caretaker.undo(fileWriter);				//checking file content again		System.out.println(fileWriter+"\n\n");			}}

Output of above memento pattern example test program is:

上面的记忆模式示例测试程序的输出为:

First Set of DataFirst Set of DataSecond Set of DataFirst Set of Data

Memento pattern is simple and easy to implement, one of the thing needs to take care is that Memento class should be accessible only to the Originator object. Also in client application, we should use caretaker object for saving and restoring the originator state.

Memento模式是简单且易于实现的,需要注意的一件事是Memento类应该只能由Originator对象访问。 同样在客户端应用程序中,我们应该使用看守对象来保存和还原发起者状态。

Also if Originator object has properties that are not immutable, we should use deep copy or cloning to avoid data integrity issue like I have used in above example. We can use to achieve memento pattern implementation that is more generic rather than Memento pattern where every object needs to have it’s own Memento class implementation.

同样,如果Originator对象具有不可变的属性,我们应该使用深层复制或克隆来避免数据完整性问题,就像我在上面的示例中使用的那样。 我们可以使用来实现更通用的memento模式实现,而不是Memento模式实现,因为每个对象都需要拥有自己的Memento类实现。

One of the drawback is that if Originator object is very huge then Memento object size will also be huge and use a lot of memory.

缺点之一是,如果Originator对象非常大,那么Memento对象的大小也会很大,并占用大量内存。

翻译自:

memento模式

转载地址:http://jwlzd.baihongyu.com/

你可能感兴趣的文章
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>