日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 雪柳花明 2016-09-25

1、備忘錄模式簡介

1.1>、定義

  備忘錄模式在不破壞封裝性的前提下,捕獲一個對象的內部狀態(tài),并在該對象之外保存這個狀態(tài)。這樣以后就可以將該對象恢復到原先保存的狀態(tài)。

1.2>、使用頻率

    

2、備忘錄模式結構

2.1>、結構圖

2.2>、參與者

  備忘錄模式參與者:

   Memento

    ° 為創(chuàng)建對象的各個部件指定抽象接口

    ° 防止Originator意外的其他對象訪問備忘錄。備忘錄實際上有兩個接口,Caretaker只能看到備忘錄的窄接口,它只能將備忘錄傳遞給其他對象。Originator能夠看到一個寬接口,允許它訪問返回到先前狀態(tài)所需的所有數據。理想的情況是只允許生成備忘錄的那個Originator訪問本備忘錄的內部狀態(tài)。

   Originator

    ° 創(chuàng)建一個備忘錄,記錄當前時刻的內部狀態(tài)。

    ° 使用備忘錄恢復內部狀態(tài)。

   Caretaker

    ° 負責保存?zhèn)渫?/p>

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

  在備忘錄模式中,Caretaker負責把Originator創(chuàng)建的Memento進行備份,當需要的時候,Originator可以再使用Caretaker中保存的Memento進行恢復,Originator中的所有狀態(tài)被恢復到備份操作之前的狀態(tài)。

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
請按任意鍵繼續(xù). . .

4、備忘錄模式應用分析

  備忘錄模式適用情形:

  1>、必須保存一個對象在某一個時刻的部分狀態(tài),這樣以后需要時才能恢復到先前的狀態(tài)。

  2>、如果用一個接口來讓其他對象直接得到被保存對象的內部狀態(tài),將會暴露對象的實現細節(jié)并破壞對象的封裝性。

  備忘錄模式特點:

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

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

分類: 設計模式

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多