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 Memento
13 {
14 //Orignation
15 class WindowsSystem
16 {
17 private string systemstate;
18 public string SystemState
19 {
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 //Memento
37 class Memento
38 {
39 private string systemstate;
40 public Memento(
string systemstate)
41 {
42 this.systemstate =
systemstate;
43 }
44 public string SystemState
45 {
46 get {
return systemstate; }
47 }
48 }
49 //Caretaker
50 class User
51 {
52 private Memento memento;
53 public Memento MementoFunc
54 {
55 get {
return memento; }
56 set { memento =
value; }
57 }
58 }
59 class Program
60 {
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 }
转载于:https://www.cnblogs.com/CheeseZH/archive/2012/05/19/2508490.html
转载请注明原文地址: https://win8.8miu.com/read-1494273.html