設計模式之備忘錄模式(Memento)__Java

來源:互聯網
上載者:User
意圖: 在不破壞封裝的前提下,捕獲一個對象的內部狀態,並在該對象之外儲存這個狀態.這樣以後就可將該對象恢複到原先儲存的狀態.
適用性: 1.必須儲存一個對象在某一個時刻的狀態,這樣以後需要時它才能恢複到先前的狀態. 2.如果一個用介面來讓其它對象直接得到這些狀態,將會暴露對象的實現細節並破壞對象的 封裝性.
效果: 1.保持封裝邊界 2.它簡化了原發器 3.使用備忘錄可能代價很高
代碼實現:
該實現為黑盒方式的單點備忘錄模式

package com.git.books.b_design_patterns.r_memento;/** * @Description: 時光機標識介面類用於提供窄介面 * @author: songqinghu * @date: 2017年3月20日 下午6:04:35 * Version:1.0 */public interface Coordinate {}

package com.git.books.b_design_patterns.r_memento;import java.text.SimpleDateFormat;import java.util.Date;/** * @Description:時光軸 表示備忘錄中的原發器 * @author: songqinghu * @date: 2017年3月20日 下午6:01:31 * Version:1.0 */public class TimeLine {    private Date date;    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    public TimeLine(Date date) {        this.date = date;    }    public Coordinate createMemento(){        return new TimeMachine(this.date);    }    public void restoreMemento(Coordinate co){        setDate(((TimeMachine)co).getDate());    }    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }    public void showDate(){        System.out.println(format.format(getDate()));    }    /**     * @Description: 時光機 用於存於狀態      * @author: songqinghu     * @date: 2017年3月20日 下午6:07:38     * Version:1.0     */    class TimeMachine implements Coordinate{        private Date date;        public TimeMachine(Date date) {            this.date = date;        }        private Date getDate() {            return date;        }//        private void setDate(Date date) {//            this.date = date;//        }    }}

package com.git.books.b_design_patterns.r_memento;/** * @Description: 上帝 負責人角色 有能力控制時間 * @author: songqinghu * @date: 2017年3月20日 下午6:13:42 * Version:1.0 */public class God {    private TimeLine timeLine;    private Coordinate co;    public God(TimeLine timeLine) {        this.timeLine = timeLine;    }    public void mementoTime(){        co = timeLine.createMemento();    }    public void retoreTime(){        timeLine.restoreMemento(co);    }}

package com.git.books.b_design_patterns.r_memento;import java.util.Date;/** * @Description: 測試類別 * @author: songqinghu * @date: 2017年3月20日 下午6:19:34 * Version:1.0 */public class MementoClient {    public static void main(String[] args) throws InterruptedException {        TimeLine timeLine = new TimeLine(new Date());        timeLine.showDate();        God god = new God(timeLine);        Thread.sleep(1000);        //設定還原點        god.mementoTime();        timeLine.setDate(new Date());        timeLine.showDate();        //還原        god.retoreTime();        timeLine.showDate();    }}

結構圖:




重點: 瞭解白盒和黑盒的備忘錄模式
參考: <<設計模式>> <<Java與模式>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.