1 /* 2 * Window系统可能会异常终止,设计一个系统备份程序。类WindowsSystem是 3 * 发起人角色(Orignation),类Memento是备忘录角色(Memento),类User是 4 * 备忘录管理角色(Caretaker)。应用备忘录模式,用C#控制台应用程序实现 5 * 该设计。 6 */ 7 using System; 8 using System.Collections.Generic; 9 using System.Linq;10 using System.Text;11 12 namespace Memento13 {14 //Orignation15 class WindowsSystem16 {17 private string systemstate;18 public string SystemState19 {20 get { return systemstate; }21 set { systemstate = value; }22 }23 public Memento SaveState()24 {25 return (new Memento(systemstate));26 }27 public void RecoveryState(Memento memeto)28 {29 this.systemstate = memeto.SystemState;30 }31 public void Show()32 {33 Console.WriteLine("当前系统状态:"+systemstate);34 }35 }36 //Memento37 class Memento38 { 39 private string systemstate;40 public Memento(string systemstate)41 {42 this.systemstate = systemstate;43 }44 public string SystemState45 {46 get { return systemstate; }47 }48 }49 //Caretaker50 class User51 {52 private Memento memento;53 public Memento MementoFunc54 {55 get { return memento; }56 set { memento = value; }57 }58 }59 class Program60 {61 static void Main(string[] args)62 {63 //系统正常64 Console.WriteLine("系统启动");65 WindowsSystem ws = new WindowsSystem();66 ws.SystemState = "正常";67 ws.Show();68 //备份69 Console.WriteLine("系统备份");70 User user = new User();71 user.MementoFunc = ws.SaveState();72 //系统崩溃73 Console.WriteLine("系统崩溃");74 ws.SystemState = "崩溃";75 ws.Show();76 //系统恢复77 Console.WriteLine("系统恢复");78 ws.RecoveryState(user.MementoFunc);79 ws.Show();80 }81 }82 }
本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/05/19/2508490.html,如需转载请自行联系原作者