【デザインモード】19、メモモード

(19)メモモード

Mementoパターンとは、オブジェクトの内部状態をキャプチャし、カプセル化を解除せずにこの状態をオブジェクトの外部に保存することを指します。スナップショットメカニズムに似ています。

1.メモパターンのデザイン原則

1.オリジネーター:保存する必要のある状態を記録するための覚書を作成する責任があります。

2. Memento:Originatorの内部状態を保存するために使用され、Originator以外のオブジェクトがアクセスするのを防ぐことができます。

3.世話人:メモの管理と作成を担当します。

2.簡単なケース

ドラフトボックスの実装はメモの形で行われ、ドラフトボックスを使用して、以前に作成したコンテンツを復元したり、無限に復元したりすることもできます。実際、一部のシナリオでは、スタックの代わりに配列の形式を使用することを検討することもできます。これにより、インデックスに従って指定されたバージョンにフォールバックできます。

public class Editor {
    
    
    private String title;
    private String content;
    private String img;

	//get,set

    //    保存功能,实际是保存到备忘录
    public ArticleMemento saveToMemento() {
    
    
        ArticleMemento articleMemento = new ArticleMemento(this.title, this.content, this.img);
        return articleMemento;
    }

//    撤销功能就是从栈中取出上一个ArticleMemento对象
    public void undoFromMemento(ArticleMemento articleMemento) {
    
    
        this.title = articleMemento.getTitle();
        this.content = articleMemento.getContent();
        this.img = articleMemento.getImg();
    }

   //全参构造
   //重写tosting
}
public class ArticleMemento {
    
    
    private String title;
    private String content;
    private String img;

    public String getTitle() {
    
    
        return title;
    }

    public String getContent() {
    
    
        return content;
    }

    public String getImg() {
    
    
        return img;
    }
    
	//全参构造
   //重写tosting
    
}
public class DraftBox {
    
    
    private final Stack<ArticleMemento> STACK = new Stack<>();

    public ArticleMemento getMemento() {
    
    
        ArticleMemento articleMemento = null;
//        业务场景一:草稿实时保存,所以撤销指的是回滚到上一版本,并不是上一步。
//        第一次弹栈是当前对象,第二次弹栈是上一个对象。
//        articleMemento = STACK.pop();
//        articleMemento = STACK.pop();

//        业务场景二:及时撤销,用户在不保存的情况下撤销,即回退到上一步。
        articleMemento = STACK.pop();

        return articleMemento;
    }

    public void addMemento(ArticleMemento articleMemento) {
    
    
        STACK.push(articleMemento);
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        DraftBox draftBox = new DraftBox();
        Editor editor = new Editor("标题1", "备忘录模式", "abc.img");
        ArticleMemento articleMemento = editor.saveToMemento();
        draftBox.addMemento(articleMemento);
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());

        System.out.println("修改标题");
        editor.setTitle("标题2");
        System.out.println("发生了修改后");
//        业务场景一:修改后并保存
       /* articleMemento = editor.saveToMemento();
        draftBox.addMemento(articleMemento);*/
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());

//        测试业务场景一
        articleMemento = draftBox.getMemento();
        editor.undoFromMemento(articleMemento);
        System.out.println("执行撤销指令后");
        System.out.println(editor.getTitle()+"\t"+editor.getContent()+"\t"+editor.getImg());
    }
}
画像-20210319090547678

3.メモモードについてのコメント

mementoモードは、ロールバックが必要なビジネスシナリオに非常に適していますが、操作オブジェクトのメンバー変数をMementoで維持する必要があるため、システムが簡単に複雑になる可能性があります。

おすすめ

転載: blog.csdn.net/qq_40589204/article/details/118511820