memento模式_Java中的Memento设计模式

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 java, memento 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 inner class 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 Data



First Set of Data
Second Set of Data



First 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 Serialization 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对象的大小也会很大,并占用大量内存。

翻译自: https://www.journaldev.com/1734/memento-design-pattern-java

memento模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值