乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#設(shè)計(jì)模式系列:備忘錄模式(Memento)

       雪柳花明 2016-09-25

      1、備忘錄模式簡(jiǎn)介

      1.1>、定義

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

      1.2>、使用頻率

          

      2、備忘錄模式結(jié)構(gòu)

      2.1>、結(jié)構(gòu)圖

      2.2>、參與者

        備忘錄模式參與者:

         Memento

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

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

         Originator

          ° 創(chuàng)建一個(gè)備忘錄,記錄當(dāng)前時(shí)刻的內(nèi)部狀態(tài)。

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

         Caretaker

          ° 負(fù)責(zé)保存?zhèn)渫?/p>

          ° 不能對(duì)備忘錄的內(nèi)容進(jìn)行操作或檢查

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

      3、備忘錄模式結(jié)構(gòu)實(shí)現(xiàn)

        Memento.cs

      復(fù)制代碼
      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; }
              }
          }
      }
      復(fù)制代碼

        Originator.cs

      復(fù)制代碼
      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;
              }
          }
      }
      復(fù)制代碼

        Caretaker.cs

      復(fù)制代碼
      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;
                  }
              }
          }
      }
      復(fù)制代碼

        Program.cs

      復(fù)制代碼
      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);
              }
          }
      }
      復(fù)制代碼

        運(yùn)行輸出:

      State = On
      State = Off
      Restoring state...
      State = On
      請(qǐng)按任意鍵繼續(xù). . .

      4、備忘錄模式應(yīng)用分析

        備忘錄模式適用情形:

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

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

        備忘錄模式特點(diǎn):

        1>、保持封裝邊界。使用備忘錄可以避免暴露一些只應(yīng)由Originator管理卻又必須存儲(chǔ)在Originator之外的信息。該模式把可能很復(fù)雜的Originator內(nèi)部信息對(duì)其他對(duì)象屏蔽起來(lái),從而保持了封裝邊界。

        2>、簡(jiǎn)化Originator。在其他的保持封裝性的設(shè)計(jì)中,Originator負(fù)責(zé)保持Client請(qǐng)求過(guò)的內(nèi)部狀態(tài)版本。把所存儲(chǔ)管理的重任交給了Originator,讓Client管理它們請(qǐng)求的狀態(tài)將會(huì)簡(jiǎn)化Originator,并使得Client工作結(jié)束時(shí)無(wú)需通知Originator。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多