日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

38备忘录模式(Memento Pattern)

發布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 38备忘录模式(Memento Pattern) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對象狀態的回溯:
??? 對象狀態的變化無端,如何回溯/恢復對象在某個點的狀態?
??? ? ? ? ? ? ? ? ? ? ??
動機:
??? 在軟件構建過程中,某些對象的狀態在轉換過程中,可能由于某種需要,要求程序能夠回溯到對象之前處于某個點時的狀態。如果使用一些公有接口來讓其他對象得到對象的狀態,便會暴露對象的細節實現。
??? 如何實現對象狀態的良好保存與恢復?但同時又不會因此而破壞對象本身的封裝性。
意圖:
在不破壞封裝性的前提下,捕獲一個對象的內部狀態,并在該對象之外保存這個狀態。這樣以后可以將該對象恢復到原先保存的狀態。

??? ?? ???
適用性:
??? 1.必須保存一個對象某一個時刻的(部分)狀態,這樣以后需要時它才能恢復到先前的狀態。
??? 2.如果一個用接口來讓其它對象直接得到這些狀態,將會暴露對象的實現細節并破壞對象的封裝性。

代碼實現:

?1?????class?Memento
?2?????{
?3?????????private?string?name;
?4?????????private?string?phone;
?5?????????private?double?budget;
?6?
?7?????????//Constructor
?8?????????public?Memento(string?name,?string?phone,?double?budget)
?9?????????{
10?????????????this.name?=?name;
11?????????????this.phone?=?phone;
12?????????????this.budget?=?budget;
13?????????}
14?????????//Properties
15?????????public?string?Name
16?????????{
17?????????????get?{?return?name;?}
18?????????????set?{?name?=?value;?}
19?????????}
20?????????public?string?Phone
21?????????{
22?????????????get?{?return?phone;?}
23?????????????set?{?phone?=?value;?}
24?????????}
25?????????public?double?Budget
26?????????{
27?????????????get?{?return?budget;?}
28?????????????set?{?budget?=?value;?}
29?????????}
30?????}

?

?1?????class?ProspectMemory
?2?????{
?3?????????private?Memento?memento;
?4?
?5?????????//Property
?6?????????public?Memento?Memento
?7?????????{
?8?????????????set?{?memento?=?value;?}
?9?????????????get?{?return?memento;?}
10?????????}
11?????}

?

?1???//Originator
?2?????class?SalesProspect
?3?????{
?4?????????private?string?name;
?5?????????private?string?phone;
?6?????????private?double?budget;
?7?
?8?????????//Properties
?9?????????public?string?Name
10?????????{
11?????????????get?{?return?name;?}
12?????????????set?{?name?=?value;?Console.WriteLine("Name:"?+?name);?}
13?????????}
14?????????public?string?Phone
15?????????{
16?????????????get?{?return?phone;?}
17?????????????set?{?phone?=?value;?Console.WriteLine("Phone:"?+?phone);?}
18?????????}
19?????????public?double?Budget
20?????????{
21?????????????get?{?return?Budget;?}
22?????????????set?{?budget?=?value;?Console.WriteLine("Budget:"?+?budget);?}
23?????????}
24?????????public?Memento?SaveMemento()
25?????????{
26?????????????Console.WriteLine("\nSaving?state?--\n");
27?????????????return?new?Memento(name,?phone,?budget);
28?????????}
29?????????public?void?RestoreMemento(Memento?memento)
30?????????{
31?????????????Console.WriteLine("\nRestoring?state?--\n");
32?????????????this.Name?=?memento.Name;
33?????????????this.Phone?=?memento.Phone;
34?????????????this.Budget?=?memento.Budget;
35?????????}
36?????}

?

?1????class?Program
?2?????{
?3?????????static?void?Main(string[]?args)
?4?????????{
?5?????????????SalesProspect?s?=?new?SalesProspect();
?6?????????????s.Name?=?"xiaoming";
?7?????????????s.Phone?=?"(010)65236523";
?8?????????????s.Budget?=?28000.0;
?9?
10?????????????//Store?internal?state
11?????????????ProspectMemory?m?=?new?ProspectMemory();
12?????????????m.Memento?=?s.SaveMemento();
13?
14?????????????//Continue?changing?originator
15?????????????s.Name?=?"deke";
16?????????????s.Phone?=?"(029)85423657";
17?????????????s.Budget?=?80000.0;
18?
19?????????????//Restore?saved?state?
20?????????????s.RestoreMemento(m.Memento);
21?
22?????????????//Wait?for?user?
23?????????????Console.Read();
24?????????}
25?????}


Memento需要注意的幾個要點:
??? 1.備忘錄(Memento)存儲原發器(Originator)對象的內部狀態,在需要時恢復原發器狀態。Memento模式適用于“由原發器管理,卻又必須存儲在原發器之外的信息”。
??? 2.在實現Memento模式中,要防止原發器以外的對象訪問備忘錄對象。備忘錄對象有兩個接口,一個為原發器的寬接口;一個為其他對象使用的窄接口。
??? 3.在實現Memento模式時,要考慮拷貝對象狀態的效率問題,如果對象開銷比較大,可以采用某種增量式改變來改進Memento模式。

總結

以上是生活随笔為你收集整理的38备忘录模式(Memento Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。