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 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();}@Overridepublic 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 objectthis.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 filecaretaker.save(fileWriter);//now write something elsefileWriter.write("Second Set of Data\n");//checking file contentsSystem.out.println(fileWriter+"\n\n");//lets undo to last savecaretaker.undo(fileWriter);//checking file content againSystem.out.println(fileWriter+"\n\n");}}

Output of above memento pattern example test program is:

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

First Set of DataFirst Set of Data
Second 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 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模式

memento模式_Java中的Memento设计模式相关推荐

  1. memento模式_Java中的Memento设计模式-示例教程

    memento模式 记忆模式是行为设计模式之一 . 当我们要保存对象的状态以便以后可以恢复时,可以使用Memento设计模式. 使用Memento模式以这种方式实现该目的,即无法在对象外部访问对象的已 ...

  2. java memento_memento模式_Java中的Memento设计模式-示例教程

    memento模式 记忆模式是行为设计模式之一 . 当我们要保存对象的状态以便以后可以恢复时,可以使用Memento设计模式. 使用Memento模式以这种方式实现该目的,即无法在对象外部访问对象的已 ...

  3. java命令模式_Java中的命令设计模式

    java命令模式 在本教程中,我们将学习命令模式,这是一种重要的行为设计模式. 它具有一些重要的应用程序,例如在文本编辑器中实现撤消/重做功能. 在命令设计模式中,有一个命令对象位于发送方和接收方对象 ...

  4. java设计模式迭代器模式_Java中的迭代器设计模式–示例教程

    java设计模式迭代器模式 迭代器模式是一种行为模式,它用于提供遍历一组对象的标准方式. Iterator模式在Java Collection Framework中得到了广泛使用,其中Iterator ...

  5. java设计模式工厂模式_Java中的工厂设计模式

    java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...

  6. java设计模式工厂模式_Java中的复合设计模式

    java设计模式工厂模式 Composite pattern is one of the Structural design pattern. Composite design pattern is ...

  7. java设计模式工厂模式_Java中的桥梁设计模式

    java设计模式工厂模式 Today we will look into Bridge Design Pattern in java. When we have interface hierarchi ...

  8. java设计模式迭代器模式_Java中的迭代器设计模式

    java设计模式迭代器模式 Iterator design pattern in one of the behavioral pattern. Iterator pattern is used to ...

  9. java设计模式代理模式_Java中的代理设计模式

    java设计模式代理模式 代理对象或代理对象为另一个对象提供占位符,以控制对该对象的访问. 代理充当原始对象的轻量级版本或简化版本. 它支持与原始对象相同的操作,但可以将那些请求委托给原始对象以实现它 ...

最新文章

  1. union all与空字段的一种用法
  2. founder of girton college
  3. 打印5列五颗星_13个Excel快捷打印技巧,让你熟练掌握打印机操作
  4. LeetCode 846. 一手顺子(map)
  5. 软件用户体验非常好的前端设计
  6. 做决定前别拍脑袋:两个成功案例看懂A/B测试
  7. 【oracle】除数为0
  8. bootstrap-validation 对表单进行比较全的验证
  9. sap abap开发从入门到精通_云端的ABAP Restful服务开发
  10. 【元胞自动机】基于元胞自动机多车道信号交叉口仿真含Matlab源码
  11. 苹果电脑(Mac)如何进行大小写和中英文的切换
  12. 如果iis的配置文件 applicationHost.config坏掉了, 会在 C:\inetpub\history\ 中存储历史备份。复制过去还原就可以了-摘自网络...
  13. 使用ffmpeg直播推流总结
  14. 使用sessionStorage实现页面间传值与传对象
  15. 最棒的 7 个 Laravel admin 后台管理系统推荐 - 卡拉云
  16. 华为华三常用display命令
  17. 苹果id登录_英雄联盟手游是用苹果ID好还是拳头好 账号选择建议
  18. C++基本功和 Design Pattern系列 Operator 下
  19. 【数据结构基础/接口函数编写】二叉树第一弹之树和堆的概念和结构、基础堆接口函数的实现(编写思路加逻辑分析加代码实操,一应俱全的汇总)
  20. JavaWeb学习-监听器

热门文章

  1. 【POJ】【3164】Commond Network
  2. 注意地方hadoop中的pi值计算
  3. inner/left/right inner
  4. [转载] python中的bin()方法
  5. [转载] python3文档字符串_python3基础:字符串、文本文件
  6. 图像滤镜艺术---Wave滤镜
  7. Java——安全地停止线程
  8. 归纳下js面向对象的几种常见写法
  9. fshc模块fsch2mcu_if理解
  10. IOS view的圆角和阴影并存