Discuss / Java / 有些太简单了,没有感觉,虽然是保存状态恢复状态,但是主要应该说明类之间的交互关系,这样才是行为型设计模式应该说明和引导理解的关键点。

有些太简单了,没有感觉,虽然是保存状态恢复状态,但是主要应该说明类之间的交互关系,这样才是行为型设计模式应该说明和引导理解的关键点。

Topic source

有些太简单了,没有感觉,虽然是保存状态恢复状态,但是主要应该说明类之间的交互关系,这样才是行为型设计模式应该说明和引导理解的关键点。

这个看着更完善一些,但还在研究中。得多看几遍才能理解含义。

https://refactoringguru.cn/design-patterns/memento

简单示例,描述 Originator 和 Memento 和Caretaker 的交互。

snapshot(Memento )

package demo.designpattern.act.memento.editor;
public final class Snapshot {
    private Editor editor;
    private String text, curX, curY, selectionWidth;
    public Snapshot(Editor editor, String text, String curX, String curY, String selectionWidth) {
        this.editor = editor;
        this.text = text;
        this.curX = curX;
        this.curY = curY;
        this.selectionWidth = selectionWidth;
    }
    public void restore() {
        editor.setText(text);
        editor.setPosition(curX, curY);
        editor.setSelectionWidth(selectionWidth);
    }
}

Originator 

package demo.designpattern.act.memento.editor;
public class Editor {
    private String text, curX, curY, selectionWidth;
    public void setText(String text) {
        this.text = text;
    }
    public void setPosition(String x, String y) {
        this.curX = x;
        this.curY = y;
    }
    public void setSelectionWidth(String selectionWidth) {
        this.selectionWidth = selectionWidth;
    }
    public void createSnapshot() {
        Snapshot snapshot = new Snapshot(this, text, curX, curY, selectionWidth);
        History.push(snapshot);
    }
    public void restore() {
        Snapshot pop = History.pop();
        if (pop != null) {
            pop.restore();
        }
    }
    @Override
    public String toString() {
        return "Editor{" +
                "text='" + text + '\'' +
                ", curX='" + curX + '\'' +
                ", curY='" + curY + '\'' +
                ", selectionWidth='" + selectionWidth + '\'' +
                '}';
    }
}

Caretaker

package demo.designpattern.act.memento.editor;
import java.util.LinkedList;
public final class History {
    private final static LinkedList<Snapshot> snapshots = new LinkedList<>();
    public static boolean push(Snapshot snapshot) {
        return snapshots.offer(snapshot);
    }
    public static Snapshot pop() {
        return snapshots.poll();
    }
}

测试demo

package demo.designpattern.act.memento.editor;
public class demo {
    public static void main(String[] args) {
        Editor editor = new Editor();
        editor.setText("修改前");
        editor.setPosition("1","2");
        editor.setSelectionWidth("12");
        editor.createSnapshot();
        System.out.println(editor.toString());
        System.out.println("---------------------");
        editor.setText("修改后");
        editor.setPosition("2","1");
        editor.setSelectionWidth("34");
        System.out.println(editor.toString());
        System.out.println("---------------------");
        editor.restore();
        System.out.println(editor.toString());
    }
}

测试输出

Editor{text='修改前', curX='1', curY='2', selectionWidth='12'}

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

Editor{text='修改后', curX='2', curY='1', selectionWidth='34'}

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

Editor{text='修改前', curX='1', curY='2', selectionWidth='12'}


  • 1

Reply