設計模式學習筆記二十——Memento模式

來源:互聯網
上載者:User
動機:在不破壞封裝性的前提下,捕獲一個對象的內部狀態,並在該對象之外儲存這個狀態。這樣就可以將該對象恢複到原先儲存的狀態。

情境:此處以圖形系統為例,圓為原發器,該系統對圓進行處理,並儲存可恢複狀態序列。

結構圖

代碼

namespace DesignPattern.Memento
{
    /**//// <summary>
    /// 原發器
    /// </summary>
    public class Round 
    {
        private Point centerPoint;
        private int radius;

        public Round(Point centerPoint, int radius)
        {
            this.centerPoint = centerPoint;
            this.radius = radius;
        }

        public RoundMemento CreateMemento()
        {
            RoundMemento memento = new RoundMemento();
            memento.SetState(this.centerPoint, this.radius);
            return memento;
        }

        public void SetMemento(RoundMemento memento)
        {
            this.centerPoint = memento.centerPoint;
            this.radius = memento.radius;
        }

        public void Move(Point point)
        {
        }

        public void SetLength(int radius)
        {
        }
    }

    /**//// <summary>
    /// 備忘錄
    /// </summary>
    public class RoundMemento
    {
        internal Point centerPoint;
        internal int radius;

        public RoundMemento()
        {
        }

        internal void SetState(Point centerPoint, int radius)
        {
            this.centerPoint = centerPoint;
            this.radius = radius;
        }
    }
}

namespace DesignPattern.Memento
{
    public class GraphicsSystem
    {
        Round round = new Round (new Point(), 10);
        IList<RoundMemento> mementos;

        public GraphicsSystem()
        {
            mementos = new List<RoundMemento>();
        }

        public void Process()
        {
            mementos.Add(round.CreateMemento());
            // 操作圓
        }

        public void Undo(int index)
        {
            round.SetMemento(mementos[index]);
        }
    }
}

要點
        1、備忘錄(Memento)儲存原發器(Originator)對象的內部狀態,在需要時恢複原發器狀態。本模式適用於“由原發器管理,卻又必須儲存在原發器之外的資訊”。
        2、實現本模式時,要防止原發器以外的對象訪問備忘錄對象。備忘錄對象有兩個介面,一個為原發器使用的寬介面,一個為其他對象使用的窄介面。
        3、實現本模式時,要考慮拷貝對象狀態的效率問題,如果對象開銷比較大,可以採用某種增量式改變來改進本模式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.