C#設計模式系列:備忘錄模式(Memento)

libingql發表於2014-04-07

1、備忘錄模式簡介

1.1>、定義

  備忘錄模式在不破壞封裝性的前提下,捕獲一個物件的內部狀態,並在該物件之外儲存這個狀態。這樣以後就可以將該物件恢復到原先儲存的狀態。

1.2>、使用頻率

   

2、備忘錄模式結構

2.1>、結構圖

2.2>、參與者

  備忘錄模式參與者:

  ◊ Memento

    ° 為建立物件的各個部件指定抽象介面

    ° 防止Originator意外的其他物件訪問備忘錄。備忘錄實際上有兩個介面,Caretaker只能看到備忘錄的窄介面,它只能將備忘錄傳遞給其他物件。Originator能夠看到一個寬介面,允許它訪問返回到先前狀態所需的所有資料。理想的情況是隻允許生成備忘錄的那個Originator訪問本備忘錄的內部狀態。

  ◊ Originator

    ° 建立一個備忘錄,記錄當前時刻的內部狀態。

    ° 使用備忘錄恢復內部狀態。

  ◊ Caretaker

    ° 負責儲存備忘錄

    ° 不能對備忘錄的內容進行操作或檢查

  在備忘錄模式中,Caretaker負責把Originator建立的Memento進行備份,當需要的時候,Originator可以再使用Caretaker中儲存的Memento進行恢復,Originator中的所有狀態被恢復到備份操作之前的狀態。

3、備忘錄模式結構實現

  Memento.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.MementoDesignPattern.Structural
{
    public class Memento
    {
        private string _state;

        public Memento(string state)
        {
            this._state = state;
        }

        public string State
        {
            get { return _state; }
        }
    }
}

  Originator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.MementoDesignPattern.Structural
{
    public class Originator
    {
        private string _state;

        public string State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                Console.WriteLine("State = " + _state);
            }
        }

        public Memento CreateMemento()
        {
            return (new Memento(_state));
        }

        public void SetMemento(Memento memento)
        {
            Console.WriteLine("Restoring state...");
            State = memento.State;
        }
    }
}

  Caretaker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.MementoDesignPattern.Structural
{
    public class Caretaker
    {
        private Memento _memento;

        public Memento Memento
        {
            get
            {
                return _memento;
            }
            set
            {
                _memento = value;
            }
        }
    }
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DesignPatterns.MementoDesignPattern.Structural;

namespace DesignPatterns.MementoDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Originator o = new Originator();
            o.State = "On";

            Caretaker c = new Caretaker();
            c.Memento = o.CreateMemento();

            o.State = "Off";
            o.SetMemento(c.Memento);
        }
    }
}

  執行輸出:

State = On
State = Off
Restoring state...
State = On
請按任意鍵繼續. . .

4、備忘錄模式應用分析

  備忘錄模式適用情形:

  1>、必須儲存一個物件在某一個時刻的部分狀態,這樣以後需要時才能恢復到先前的狀態。

  2>、如果用一個介面來讓其他物件直接得到被儲存物件的內部狀態,將會暴露物件的實現細節並破壞物件的封裝性。

  備忘錄模式特點:

  1>、保持封裝邊界。使用備忘錄可以避免暴露一些只應由Originator管理卻又必須儲存在Originator之外的資訊。該模式把可能很複雜的Originator內部資訊對其他物件遮蔽起來,從而保持了封裝邊界。

  2>、簡化Originator。在其他的保持封裝性的設計中,Originator負責保持Client請求過的內部狀態版本。把所儲存管理的重任交給了Originator,讓Client管理它們請求的狀態將會簡化Originator,並使得Client工作結束時無需通知Originator。

相關文章