CSharp: Memento Pattern in donet core 6

 

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Geovin.Du.DuMemento.Conceptual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuMemento.FoodSupplier
{
 
    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class FoodSupplier
    {
 
        /// <summary>
        ///
        /// </summary>
        private string _name = string.Empty;
        /// <summary>
        ///
        /// </summary>
        private string _phone = string.Empty;
        /// <summary>
        ///
        /// </summary>
        private string _address = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                Console.WriteLine($"食品供应商:名称设置为 {_name}");
            }
        }
        /// <summary>
        ///
        /// </summary>
        public string Phone
        {
            get => _phone;
            set
            {
                _phone = value;
                Console.WriteLine($"食品供应商:电话号码设置为 {_phone}");
            }
        }
        /// <summary>
        ///
        /// </summary>
        public string Address
        {
            get => _address;
            set
            {
                _address = value;
                Console.WriteLine($"食品供应商:地址设置为{_address}");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"姓名:{Name} 电话:{Phone} 地址:{ Address}";
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IMemento Save()
        {
            Console.WriteLine("\n食品供应商:保存现状...");
            return new FoodSupplierMemento(_name, _phone, _address);
        }
 
        /// <summary>
        /// Restore previous state.
        /// Note that the method returns an IMemento instance in order to to facilitate redo operation.
        /// The return type can be changed to void if redo doesn't have to be supported.
        /// </summary>
        /// <param name="memento">Memento for restoration.</param>
        /// <returns>Redo memento.</returns>
        public IMemento Restore(IMemento memento)
        {
            Console.WriteLine("\n食品供应商:恢复状态...");
 
            if (memento is not FoodSupplierMemento foodSupplierMemento)
            {
                throw new ArgumentException($"未知的纪念品: {memento}");
            }
 
            var redoMemento = new FoodSupplierMemento(_name, _phone, _address);
 
            Name = foodSupplierMemento.Name;
            Phone = foodSupplierMemento.PhoneNumber;
            Address = foodSupplierMemento.Address;
 
            return redoMemento;
        }
    }
 
}
 
 
using Geovin.Du.DuMemento.Conceptual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuMemento.FoodSupplier
{
 
    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class SupplierRegistry
    {
 
        /// <summary>
        ///
        /// </summary>
        private readonly FoodSupplier _supplier;
        /// <summary>
        ///
        /// </summary>
        private readonly Stack<IMemento> _undoStack;
        /// <summary>
        ///
        /// </summary>
        private readonly Stack<IMemento> _redoStack;
        /// <summary>
        ///
        /// </summary>
        /// <param name="supplier"></param>
        public SupplierRegistry(FoodSupplier supplier)
        {
            _supplier = supplier;
            _undoStack = new Stack<IMemento>();
            _redoStack = new Stack<IMemento>();
        }
        /// <summary>
        ///
        /// </summary>
        public void Backup()
        {
            Console.WriteLine("\n供应商注册表:备份操作..");
 
            _redoStack.Clear();
            _undoStack.Push(_supplier.Save());
        }
        /// <summary>
        ///
        /// </summary>
        public void Undo()
        {
            Console.WriteLine("\n供应商注册表:正在执行撤销操作...");
 
            var top = _undoStack.Pop();
            _redoStack.Push(_supplier.Restore(top));
        }
        /// <summary>
        ///
        /// </summary>
        public void Redo()
        {
            Console.WriteLine("\n供应商注册表:执行重做操作...");
 
            var top = _redoStack.Pop();
            _undoStack.Push(_supplier.Restore(top));
        }
    }
}
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuMemento.FoodSupplier
{
 
    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public interface IMemento
    {
    }
}
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Geovin.Du.DuMemento.FoodSupplier
{
 
    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class FoodSupplierMemento : IMemento
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="address"></param>
        public FoodSupplierMemento(string name, string phoneNumber, string address)
        {
            Name = name;
            PhoneNumber = phoneNumber;
            Address = address;
        }
        /// <summary>
        ///
        /// </summary>
        public string Name { get; set; }
 
        /// <summary>
        /// /
        /// </summary>
        public string PhoneNumber { get; set; }
        /// <summary>
        ///
        /// </summary>
        public string Address { get; set; }
    }
 
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.BuildingBlocks;
 
 
namespace Geovin.Du.DuMemento.FoodSupplier
{
 
    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public static class FoodSupplierExecutor
    {
 
 
        /// <summary>
        ///
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("\n备忘录模式 Memento Pattern 食物供应 Food supplier demo");
 
            var foodSupplier = new FoodSupplier
            {
                Name = "涂氏食品工业公司",
                Phone = "8888 1111",
                Address = "深圳市罗湖区",
            };
 
            var registry = new SupplierRegistry(foodSupplier);
            registry.Backup();
 
            foodSupplier.Phone = "8888 2222";
 
            registry.Undo();
            registry.Redo();
        }
    }
}

  

 

调用:

1
Geovin.Du.DuMemento.FoodSupplier.FoodSupplierExecutor.Execute();

  

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
备忘录模式 Memento Pattern 食物供应 Food supplier demo
--------------------------------------------------
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 1111
食品供应商:地址设置为深圳市罗湖区
 
供应商注册表:备份操作..
 
食品供应商:保存现状...
食品供应商:电话号码设置为 8888 2222
 
供应商注册表:正在执行撤销操作...
 
食品供应商:恢复状态...
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 1111
食品供应商:地址设置为深圳市罗湖区
 
供应商注册表:执行重做操作...
 
食品供应商:恢复状态...
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 2222
食品供应商:地址设置为深圳市罗湖区

  

posted @   ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2009-12-19 javascript有声调的汉字注音字典(兼容各浏览器)
2005-12-19 传值调用与引用调用
2005-12-19 属性、方法作用范围
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示