Java設計モード(21)覚書モード
10994 ワード
メモモード
定義#テイギ#
スナップショットモードまたはトークンモードとも呼ばれ、パッケージを破壊しない前提で、1つのオブジェクトの内部状態が悪く、オブジェクトの外にこの状態を保存することを指す.これにより、オブジェクトを元の保存状態に戻すことができ、動作モードに属します.
ソフトウェアシステムでは、メモモードは、システムの各履歴状態のスナップショットを格納することによって、システムを任意の時点で履歴状態にロールバックできる「後悔薬」のメカニズムを提供します.
適用シナリオ履歴スナップショットを保存する必要があるシーン は、オブジェクト以外にステータスを保存し、他のクラスのオブジェクトがアクセスできない状態で特定のコンテンツを保存することを望んでいる.
ロール#ロール#発起人(Originator):自分が保存する必要がある状態を記録するメモを作成します.ステータスロールバック機能を備えています. メモ(Memento):Originatorの内部状態が格納されているため、Originator以外のオブジェクトへのアクセスが防止される. メモ管理者(Caretaker):メモの保存、管理を担当し、メモの内容を操作およびアクセスできません.
≪インスタンス|Instance|emdw≫
スタックを使用して着地メモモードを管理する
メリット発起人実体クラス(Originator)の職責を簡略化し、状態記憶と取得を隔離し、情報のカプセル化を実現し、クライアントは関係状態の保存詳細を必要としない. は、ステータスロールバック機能を提供する.
欠点
≪リソースの消費|消耗Resources|emdw≫:保存するステータスが多すぎる場合は、保存するたびに多くのメモリが消費されます.
定義#テイギ#
スナップショットモードまたはトークンモードとも呼ばれ、パッケージを破壊しない前提で、1つのオブジェクトの内部状態が悪く、オブジェクトの外にこの状態を保存することを指す.これにより、オブジェクトを元の保存状態に戻すことができ、動作モードに属します.
ソフトウェアシステムでは、メモモードは、システムの各履歴状態のスナップショットを格納することによって、システムを任意の時点で履歴状態にロールバックできる「後悔薬」のメカニズムを提供します.
適用シナリオ
ロール#ロール#
≪インスタンス|Instance|emdw≫
スタックを使用して着地メモモードを管理する
public class ArticleMemento {
private String title;
private String content;
private String imgs;
public ArticleMemento(String title, String content, String imgs) {
this.title = title;
this.content = content;
this.imgs = imgs;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getImgs() {
return imgs;
}
@Override
public String toString() {
return "ArticleMemento{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", imgs='" + imgs + '\'' +
'}';
}
}
public class Editor {
private String title;
private String content;
private String imgs;
public Editor(String title, String content, String imgs) {
this.title = title;
this.content = content;
this.imgs = imgs;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImgs() {
return imgs;
}
public void setImgs(String imgs) {
this.imgs = imgs;
}
public ArticleMemento saveToMemento(){
ArticleMemento articleMemento = new ArticleMemento(this.title,this.content, this.imgs);
return articleMemento;
}
public void undoFromMemento(ArticleMemento articleMemento){
this.title = articleMemento.getTitle();
this.content = articleMemento.getContent();
this.imgs = articleMemento.getImgs();
}
@Override
public String toString() {
return "Editor{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", imgs='" + imgs + '\'' +
'}';
}
}
public class DraftsBox {
private final Stack<ArticleMemento> STACK = new Stack<ArticleMemento>();
public ArticleMemento getMemento(){
ArticleMemento articleMemento = STACK.pop();
return articleMemento;
}
public void addMemento(ArticleMemento articleMemento){
STACK.push(articleMemento);
}
}
public class Test {
public static void main(String[] args) {
DraftsBox draftsBox = new DraftsBox();
Editor editor = new Editor(" "," ","asdf.png");
ArticleMemento articleMemento = editor.saveToMemento();
draftsBox.addMemento(articleMemento);
}
}
メリット
欠点
≪リソースの消費|消耗Resources|emdw≫:保存するステータスが多すぎる場合は、保存するたびに多くのメモリが消費されます.