Sample code for Java Memento pattern implementation
/** * 数据对象 * @author stone * */ public class DataState { private String action; public void setAction(String action) { this.action = action; } public String getAction() { return action; } }
/** * 一个保存另外一个对象内部状态拷贝 的对象.这样以后就可以将该对象恢复到原先保存的状态. * * @author stone * */ import java.io.File; import java.io.Serializable; public class Memento implements Serializable { /*private int number; private File file = null; public Memento(Originator o) { this.number = o.getNumber(); this.file = o.getFile(); } public int getNumber() { return this.number; } public void setNumber(int number) { this.number = number; } public File getFile() { return this.file; } public void setFile(File file) { this.file = file; } */ private DataState state; public Memento(Originator o) { this.state = o.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } }
public class Originator { /* private int number; private File file = null; public Originator() { } // 创建一个Memento,将自身作为参数传入 public Memento getMemento() { return new Memento(this); } // 从Memento中取出保存的数据,恢复为原始状态 public void setMemento(Memento m) { number = m.getNumber(); file = m.getFile(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public File getFile() { return file; } public void setFile(File file) { this.file = file; }*/ private DataState state; public Originator() { } public Originator(DataState state) { this.state = state; } // 创建一个Memento,将自身作为参数传入 public Memento getMemento() { return new Memento(this); } // 从Memento中取出保存的数据,恢复为原始状态 public void setMemento(Memento m) { /* * getMemento() 创建的对象,保存在某个容器里, * 当需要恢复时,将其传入当前方法, 再使用getState(),得出 */ this.state = m.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } }
/* * 备忘录(Memento)模式 行为型模式 * 流程: Memento用于保存 数据状态, * Originator用于 加载数据, 建立Memento对象,及通过Memento恢复原始数据 */ public class Test { public static void main(String[] args) { // Originator originator = new Originator(); // originator.setNumber(8); // // Memento memento = originator.getMemento(); // System.out.println(memento.getNumber()); DataState state = new DataState(); state.setAction("copy a character"); Originator originator = new Originator(); System.out.println("创建原始数据"); originator.setState(state); System.out.println("创建备忘录对象, 保存原始数据状态"); Memento memento = originator.getMemento(); System.out.println("创建了一个新数据"); originator.setState(new DataState()); System.out.println("创建新数据后:" + originator.getState().getAction()); /* * memento 需要保存在某地,需要时取出,以恢复它内部所保存的数据 */ System.out.println("创建新数据后,恢复原数据"); originator.setMemento(memento); System.out.println(originator.getState().getAction()); } }
创建原始数据 创建备忘录对象, 保存原始数据状态 创建了一个新数据 创建新数据后:null 创建新数据后,恢复原数据 copy a character
The above is the detailed content of Sample code for Java Memento pattern implementation. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
