天天看點

Net設計模式執行個體之備忘錄模式(Memento Pattern)

備忘錄模式(Memento Pattern),在不破壞封裝的前提下,捕獲一個對象的内部狀态,并在該對象之外儲存這個狀态。這樣以後就可以就該對象恢複到原先儲存的狀态。

       當系統功能比較複雜,而且需要記錄曆史屬性以便當需要時做恢複動作。Originator可以根據儲存的Memento資訊還原到前一狀态。   

Net設計模式執行個體之備忘錄模式(Memento Pattern)

Originator類:發起人。

CreateMemento方法,負責建立一個備忘錄,用于記錄目前時刻它的内部狀态。

SetMemento方法,使用備忘錄回複狀态。

Show方法,展示狀态。

Originator類:可以決定備忘錄Memento存儲Originator的哪些狀态。

Memento類:備忘錄,負責存儲Originator的内部狀态,并可防止Originator以外的其他對象通路備忘錄Memento

Caretaker類:負責儲存備忘錄Memento,不能對備忘錄的内容進行操縱和檢查。

1、發起人類Originator

public class Originator

{

    private string _state;

    public string State

    {

        get { return _state; }

        set { _state = value; }

    }

    /// <summary>

    /// 建立備忘錄,将目前要儲存的資訊導入并執行個體化備忘錄

    /// </summary>

    public Memento CreateMemento()

        return (new Memento(this.State));

    /// 恢複備忘錄,将Memento導入并将相關資料恢複

    /// <param name="memento"></param>

    public void SetMemento(Memento memento)

        this.State = memento.State;

    /// 展示狀态資料

    public void Show()

        Console.WriteLine("目前狀态是:"+this.State);

}

2、備忘錄類Memento

public class Memento

    public Memento(string state)

        this.State = state;

3、管理者類Caretaker

public class Caretaker

    private Memento _memento;

    public Memento Memento

        get { return _memento; }

        set { _memento = value; }

4、用戶端代碼

static void Main(string[] args)

    Originator o = new Originator();

    //初始狀态為On

    o.State = "On";

    o.Show();

    //建立備忘錄并儲存狀态

    Caretaker caretaker = new Caretaker();

    caretaker.Memento=o.CreateMemento();

    //更改Originator狀态=Off

    o.State = "Off";

    //恢複到原始狀态

    o.SetMemento(caretaker.Memento);

    Console.ReadKey();

Net設計模式執行個體之備忘錄模式(Memento Pattern)

首先定義銷售代理Noel van Halen的相關資訊.然後儲存到備忘錄中,而定義銷售代理Leo Welch相關資訊。然後又回覆前一代理Noel van Halen的資訊。。結構如下圖所示

Net設計模式執行個體之備忘錄模式(Memento Pattern)

SalesProspect類:發起人

SaveMemento方法:建立備忘錄

RestoreMemento方法:回覆備忘錄

Memento類:備忘錄,需要備份的資訊有姓名、電話和預算

ProspectMemory類:負責儲存備忘錄Memento

1、發起人類SalesProspect

class SalesProspect

    private string _name;

    private string _phone;

    private double _budget;

    public string Name

    {

        get { return _name; }

        set

        {

            _name = value;

            Console.WriteLine("Name:  " + _name);

        }

    public string Phone

        get { return _phone; }

            _phone = value;

            Console.WriteLine("Phone: " + _phone);

    public double Budget

        get { return _budget; }

            _budget = value;

            Console.WriteLine("Budget: " + _budget);

    public Memento SaveMemento()

        Console.WriteLine("\nSaving state --\n");

        return new Memento(_name, _phone, _budget);

    public void RestoreMemento(Memento memento)

        Console.WriteLine("\nRestoring state --\n");

        this.Name = memento.Name;

        this.Phone = memento.Phone;

        this.Budget = memento.Budget;

class Memento

    public Memento(string name, string phone, double budget)

        this._name = name;

        this._phone = phone;

        this._budget = budget;

        set { _name = value; }

        set { _phone = value; }

        set { _budget = value; }

3、管理者類ProspectMemory

class ProspectMemory

    // Property

3、用戶端代碼

    SalesProspect s = new SalesProspect();

    s.Name = "Noel van Halen";

    s.Phone = "(412) 256-0990";

    s.Budget = 25000.0;

    // Store internal state

    ProspectMemory m = new ProspectMemory();

    m.Memento = s.SaveMemento();

    // Continue changing originator

    s.Name = "Leo Welch";

    s.Phone = "(310) 209-7111";

    s.Budget = 1000000.0;

    // Restore saved state

    s.RestoreMemento(m.Memento);

Net設計模式執行個體之備忘錄模式(Memento Pattern)

本文對備忘錄模式設計思想、結構和結構代碼進行了分析,并以一執行個體進一步闡述了備忘錄模式的C#實作。

版權

作者:靈動生活 郝憲玮

如果你認為此文章有用,請點選底端的【推薦】讓其他人也了解此文章,

Net設計模式執行個體之備忘錄模式(Memento Pattern)

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

繼續閱讀