CSharp: Memento Pattern in donet core 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | /// <summary> /// 备忘录模式 Memento Pattern 亦称: 快照、Snapshot、Memento /// geovindu,Geovin Du edit /// </summary> class Originator { /// <summary> /// 状态 /// </summary> private string state; /// <summary> /// Memento myMemento;//not needed now /// </summary> public Originator() { //Creating a memento with born state. state = "快照 #0.(出生状态)" ; Console.WriteLine($ "发起人的当前状态是: {state}" ); } /// <summary> /// 状态 /// </summary> public string State { get { return state; } set { state = value; Console.WriteLine($ "发起人的的当前状态是: {state}" ); } } /// <summary> /// /// </summary> /// <returns></returns> public Memento CurrentMemento() { //Code segment used in Demonstration-1 //myMemento = new Memento();//error now, because of private constructor //myMemento.State = state; //return myMemento; //Modified code for Demonstration-2 return new Memento( this .State); } /// <summary> /// Back to an old state (Restore) /// </summary> /// <param name="restoreMemento"></param> public void RestoreMemento(Memento restoreMemento) { this .state = restoreMemento.State; //Console.WriteLine("Restored to state : {0}", state); Console.WriteLine($ "愎复状态是: {state}" ); } /// <summary> /// Memento class /// </summary> internal class Memento { /// <summary> /// /// </summary> private string state; //Now Memento class cannot be initialized outside /// <summary> /// /// </summary> private Memento() { } /// <summary> /// /// </summary> /// <param name="state">输入状态</param> public Memento( string state) { this .state = state; } /// <summary> /// /// </summary> public string State { get { return state; } set { state = value; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | /// <summary> /// 备忘录模式 Memento Pattern 亦称: 快照、Snapshot、Memento /// geovindu,Geovin Du edit /// </summary> public class Memento { /// <summary> /// /// </summary> public int Balance { get ; } /// <summary> /// /// </summary> /// <param name="balance"></param> public Memento( int balance) { Balance = balance; } } /// <summary> /// /// </summary> public class BankAccount // supports undo/redo { /// <summary> /// /// </summary> private int balance; /// <summary> /// /// </summary> private List<Memento> changes = new List<Memento>(); /// <summary> /// /// </summary> private int current; /// <summary> /// /// </summary> /// <param name="balance"></param> public BankAccount( int balance) { this .balance = balance; changes.Add( new Memento(balance)); } /// <summary> /// /// </summary> /// <param name="amount"></param> /// <returns></returns> public Memento Deposit( int amount) { balance += amount; var m = new Memento(balance); changes.Add(m); ++current; return m; } /// <summary> /// /// </summary> /// <param name="m"></param> public void Restore(Memento m) { if (m != null ) { balance = m.Balance; changes.Add(m); current = changes.Count - 1; } } /// <summary> /// /// </summary> /// <returns></returns> public Memento Undo() { if (current > 0) { var m = changes[--current]; balance = m.Balance; return m; } return null ; } /// <summary> /// /// </summary> /// <returns></returns> public Memento Redo() { if (current + 1 < changes.Count) { var m = changes[++current]; balance = m.Balance; return m; } return null ; } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $ "{nameof(balance)}: {balance}" ; } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | //备忘录模式 Originator originatorObject; Originator.Memento currentMemento; Console.WriteLine( "***备忘录模式Memento Pattern Demonstration-2.***" ); Console.WriteLine( "发起人 (带有嵌套的内部类“Memento”)在一个单独的文件中维护.\n" ); originatorObject = new Originator(); IList<Originator.Memento> savedStates = new List<Originator.Memento>(); /* Adding a memento the list.This memento stores the current state of the Origintor. */ savedStates.Add(originatorObject.CurrentMemento()); //Snapshot #1. originatorObject.State = "快照 #1" ; //Adding this memento as a restore point savedStates.Add(originatorObject.CurrentMemento()); //Snapshot #2. originatorObject.State = "快照 #2" ; //Adding this memento as a restore point savedStates.Add(originatorObject.CurrentMemento()); //Snapshot #3. originatorObject.State = "快照 #3" ; //Adding this memento as a restore point savedStates.Add(originatorObject.CurrentMemento()); //Snapshot #4. It is not added as a restore point. originatorObject.State = "快照 #4" ; //Available restore points Console.WriteLine( "\n当前可用的恢复点为 :" ); foreach (Originator.Memento m in savedStates) { Console.WriteLine(m.State); } //Undo's //Roll back starts... Console.WriteLine( "\n现在执行撤销." ); for ( int i = savedStates.Count; i > 0; i--) { //Get a restore point currentMemento = savedStates[i - 1]; originatorObject.RestoreMemento(currentMemento); } //Redo's Console.WriteLine( "\n现在执行重做." ); for ( int i = 1; i < savedStates.Count; i++) { currentMemento = savedStates[i]; originatorObject.RestoreMemento(currentMemento); } Console.WriteLine(); var ba = new BankAccount(100); ba.Deposit(50); ba.Deposit(25); Console.WriteLine(ba); ba.Undo(); Console.WriteLine($ "Undo geovindu #1: {ba}" ); ba.Undo(); Console.WriteLine($ "Undo geovindu #2: {ba}" ); ba.Redo(); Console.WriteLine($ "Redo geovindu #2: {ba}" ); Console.ReadKey(); |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ***备忘录模式Memento Pattern Demonstration-2.*** 发起人 (带有嵌套的内部类“Memento”)在一个单独的文件中维护. 发起人的当前状态是: 快照 #0.(出生状态) 发起人的的当前状态是: 快照 #1 发起人的的当前状态是: 快照 #2 发起人的的当前状态是: 快照 #3 发起人的的当前状态是: 快照 #4 当前可用的恢复点为 : 快照 #0.(出生状态) 快照 #1 快照 #2 快照 #3 现在执行撤销. 愎复状态是: 快照 #3 愎复状态是: 快照 #2 愎复状态是: 快照 #1 愎复状态是: 快照 #0.(出生状态) 现在执行重做. 愎复状态是: 快照 #1 愎复状态是: 快照 #2 愎复状态是: 快照 #3 balance: 175 Undo geovindu #1: balance: 150 Undo geovindu #2: balance: 100 Redo geovindu #2: balance: 150 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2014-10-10 String Control
2013-10-10 Csharp:user WebControl Read Adobe PDF Files In Your Web Browser
2009-10-10 What Is Web 2.0