/** * 發起人類 */ public class Originator { /** * 狀態編碼 */ private String status; public Originator(String status) { this.status = status; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } /** * 建立備忘錄 * @return */ public Memento createMemento() { return new Memento(this); } /** * 回滾 * @param memento */ public void restoreMemento(Memento memento) { this.status = memento.getStatus(); } } /** * 備忘錄類 */ public class Memento { private String status; public Memento(Originator originator) { this.status = originator.getStatus(); } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } } /** * 備忘錄管理類 */ public class Caretaker { /** * 備忘錄記錄棧 */ private Stack<Memento> MEMENTO_STACK; public Caretaker() { MEMENTO_STACK = new Stack<>(); } /** * 添加一個備忘錄 * @param memento */ public void addMemento(Memento memento) { MEMENTO_STACK.push(memento); } /** * 獲取一個備忘錄 * @return */ public Memento getMemento() { return MEMENTO_STACK.pop(); } }
/** * 測試與應用 */ public class Test { public static void main(String[] args) { //備忘錄管理 Caretaker caretaker = new Caretaker(); //發起人 Originator originator = new Originator("1"); //建立備忘錄1 Memento memento1 = originator.createMemento(); caretaker.addMemento(memento1); //修改並建立備忘錄2 originator.setStatus("2"); Memento memento2 = originator.createMemento(); caretaker.addMemento(memento2); //修改狀態3 originator.setStatus("3"); System.out.println(originator.getStatus()); //回滾上一次 originator.restoreMemento(caretaker.getMemento()); System.out.println(originator.getStatus()); //回滾上一次 originator.restoreMemento(caretaker.getMemento()); System.out.println(originator.getStatus()); } }
3 2 1
備忘錄模式角色介紹html
在線編輯文章時,能夠回退功能得備忘錄模式實現。
/** * 文章類 */ public class Article { //標題 private String title; //內容 private String content; //圖片 private String images; public Article(String title, String content, String images) { this.title = title; this.content = content; this.images = images; } 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 getImages() { return images; } public void setImages(String images) { this.images = images; } public ArticleMemento saveToMemento() { return new ArticleMemento(this); } public void undoFromMemento(ArticleMemento articleMemento) { this.title = articleMemento.getTitle(); this.content = articleMemento.getContent(); this.images = articleMemento.getImages(); } @Override public String toString() { return "Article{" + "title='" + title + '\'' + ", content='" + content + '\'' + ", images='" + images + '\'' + '}'; } } /** * 備忘錄類 */ public class ArticleMemento { private String title; private String content; private String images; public ArticleMemento(Article article) { this.title = article.getTitle(); this.content = article.getContent(); this.images = article.getImages(); } public String getTitle() { return title; } public String getContent() { return content; } public String getImages() { return images; } @Override public String toString() { return "ArticleMemento{" + "title='" + title + '\'' + ", content='" + content + '\'' + ", images='" + images + '\'' + '}'; } } /** * 備忘錄管理類 */ public class ArticleMementoManager { private final Stack<ArticleMemento> ARTICLE_MEMENTO_STACK = new Stack<>(); public ArticleMemento getMemento () { return ARTICLE_MEMENTO_STACK.pop(); } public void addMemento(ArticleMemento articleMemento) { ARTICLE_MEMENTO_STACK.push(articleMemento); } }
/** * 測試與應用 */ public class Test { public static void main(String[] args) { //建立備忘錄管理 ArticleMementoManager articleMementoManager = new ArticleMementoManager(); Article article = new Article("如影隨形的設計模式A", "內容A", "圖片A"); ArticleMemento articleMemento = article.saveToMemento(); articleMementoManager.addMemento(articleMemento); System.out.println(article.toString()); article.setTitle("修改手記start"); article.setContent("手記內容B"); article.setImages("手記圖片B"); System.out.println(article); articleMemento = article.saveToMemento(); articleMementoManager.addMemento(articleMemento); article.setTitle("設計模式C"); article.setContent("手記內容C"); article.setImages("手記圖片C"); System.out.println(article.toString()); System.out.println("回退出棧一次"); articleMemento = articleMementoManager.getMemento(); article.undoFromMemento(articleMemento); System.out.println(article.toString()); System.out.println("回退出棧兩次"); articleMemento = articleMementoManager.getMemento(); article.undoFromMemento(articleMemento); System.out.println(article.toString()); } }
Article{title='design-pattern1', content='memento1', images='memento1'} Article{title='design-pattern2', content='memento2', images='memento2'} Article{title='design-pattern3', content='memento3', images='memento3'} pop stack 1. Article{title='design-pattern2', content='memento2', images='memento2'} pop stack 2. Article{title='design-pattern1', content='memento1', images='memento1'}
備忘錄模式和狀態模式java
慕課網設計模式精講
:https://coding.imooc.com/class/270.html 備忘錄模式(詳盡版)
:http://c.biancheng.net/view/1400.html