Net设计模式实例之备忘录模式(Memento Pattern)
一、備忘錄模式簡介(Brief Introduction)
備忘錄模式(Memento Pattern),在不破壞封裝的前提下,捕獲一個對象的內部狀態,并在該對象之外保存這個狀態。這樣以后就可以就該對象恢復到原先保存的狀態。
二、解決的問題(What To Solve)
?????? 當系統功能比較復雜,而且需要記錄歷史屬性以便當需要時做恢復動作。Originator可以根據保存的Memento信息還原到前一狀態。???
三、備忘錄模式分析(Analysis)
1、備忘錄模式結構
Originator類:發起人。
CreateMemento方法,負責創建一個備忘錄,用于記錄當前時刻它的內部狀態。
SetMemento方法,使用備忘錄回復狀態。
Show方法,展示狀態。
Originator類:可以決定備忘錄Memento存儲Originator的哪些狀態。
Memento類:備忘錄,負責存儲Originator的內部狀態,并可防止Originator以外的其他對象訪問備忘錄Memento
Caretaker類:負責保存備忘錄Memento,不能對備忘錄的內容進行操縱和檢查。
2、備忘錄模式代碼
| 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)); ??? } ? ??? /// <summary> ??? /// 恢復備忘錄,將Memento導入并將相關數據恢復 ??? /// </summary> ??? /// <param name="memento"></param> ??? public void SetMemento(Memento memento) ??? { ??????? this.State = memento.State; ??? } ? ??? /// <summary> ??? /// 展示狀態數據 ??? /// </summary> ??? public void Show() ??? { ??????? Console.WriteLine("當前狀態是:"+this.State); ??? } } |
?
| 2、備忘錄類Memento |
| public class Memento { ??? private string _state; ? ??? public string State ??? { ??????? get { return _state; } ??????? set { _state = value; } ??? } ? ??? 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.Show(); ? ??? //恢復到原始狀態 ??? o.SetMemento(caretaker.Memento); ??? o.Show(); ? ??? Console.ReadKey(); } |
?
3、備忘錄模式運行結果
四.實例分析(Example)
1、場景
首先定義銷售代理Noel van Halen的相關信息.然后保存到備忘錄中,而定義銷售代理Leo Welch相關信息。然后又回覆前一代理Noel van Halen的信息。。結構如下圖所示
SalesProspect類:發起人
SaveMemento方法:創建備忘錄
RestoreMemento方法:回覆備忘錄
Memento類:備忘錄,需要備份的信息有姓名、電話和預算
ProspectMemory類:負責保存備忘錄Memento
2、代碼
| 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; } ??????? set ??????? { ??????????? _phone = value; ??????????? Console.WriteLine("Phone: " + _phone); ??????? } ??? } ??? public double Budget ??? { ??????? get { return _budget; } ??????? set ??????? { ??????????? _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; ??? } } |
?
| 2、備忘錄類Memento |
| class Memento { ??? private string _name; ??? private string _phone; ??? private double _budget; ??? public Memento(string name, string phone, double budget) ??? { ??????? this._name = name; ??????? this._phone = phone; ??????? this._budget = budget; ??? } ??? public string Name ??? { ??????? get { return _name; } ??????? set { _name = value; } ??? } ??? public string Phone ??? { ??????? get { return _phone; } ??????? set { _phone = value; } ??? } ??? public double Budget ??? { ??????? get { return _budget; } ??????? set { _budget = value; } ??? } } |
?
| 3、管理者類ProspectMemory |
| class ProspectMemory { ??? private Memento _memento; ??? // Property ??? public Memento Memento ??? { ??????? set { _memento = value; } ??????? get { return _memento; } ??? } } |
?
| 3、客戶端代碼 |
| static void Main(string[] args) { ??? 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); ??? Console.ReadKey(); } |
3、實例運行結果
五、總結(Summary)
本文對備忘錄模式設計思想、結構和結構代碼進行了分析,并以一實例進一步闡述了備忘錄模式的C#實現。
轉載于:https://www.cnblogs.com/ywqu/archive/2010/01/25/1655581.html
總結
以上是生活随笔為你收集整理的Net设计模式实例之备忘录模式(Memento Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 巧用SQL server的全局临时表防止
- 下一篇: 开会效果不错