• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            模式設計c#--行為型--menento

            名稱 Memento
            結構 o_menento.bmp
            意圖 在不破壞封裝性的前提下,捕獲一個對象的內部狀態,并在該對象之外保存這個狀態。這樣以后就可將該對象恢復到原先保存的狀態。
            適用性
            • 必須保存一個對象在某一個時刻的(部分)狀態, 這樣以后需要時它才能恢復到先前的狀態。
            • 如果一個用接口來讓其它對象直接得到這些狀態,將會暴露對象的實現細節并破壞對象的封裝性。


            namespace ?Memento_DesignPattern
            {
            ????
            using ?System;

            ????
            class ?Originator?
            ????
            {
            ????????
            private ? double ?manufacturer = 0 ;
            ????????
            private ? double ?distributor? = ? 0 ;
            ????????
            private ? double ?retailer? = ? 0 ;

            ????????
            public ? void ?MakeSale( double ?purchasePrice)
            ????????
            {
            ????????????
            // ?We?assume?sales?are?divided?equally?amount?the?three
            ????????????manufacturer? += ?purchasePrice? * ?. 40 ;
            ????????????distributor?
            += ?purchasePrice? * . 3 ;
            ????????????retailer?
            += ?purchasePrice? * . 3 ;
            ????????????
            // ?Note:?to?avoid?rounding?errors?for?real?money?handling?
            ????????????
            // ?apps,?we?should?be?using?decimal?integers
            ????????????
            // ?(but?hey,?this?is?just?a?demo!)
            ????????}


            ????????
            public ?Memento?CreateMemento()
            ????????
            {
            ????????????
            return ?( new ?Memento(manufacturer,?distributor,?retailer));????????????
            ????????}

            ????????
            ????????
            public ? void ?SetMemento(Memento?m)
            ????????
            {
            ????????????manufacturer?
            = ?m.A;
            ????????????distributor?
            = ?m.B;
            ????????????retailer?
            = ?m.C;
            ????????}
            ????????
            ????}


            ????
            class ?Memento?
            ????
            {
            ????????
            private ? double ?iA;
            ????????
            private ? double ?iB;
            ????????
            private ? double ?iC;

            ????????
            public ?Memento( double ?a,? double ?b,? double ?c)
            ????????
            {
            ????????????iA?
            = ?a;
            ????????????iB?
            = ?b;
            ????????????iC?
            = ?c;
            ????????}


            ????????
            public ? double ?A?
            ????????
            {
            ????????????
            get ?
            ????????????
            {
            ????????????????
            return ?iA;
            ????????????}

            ????????}


            ????????
            public ? double ?B?
            ????????
            {
            ????????????
            get ?
            ????????????
            {
            ????????????????
            return ?iB;
            ????????????}

            ????????}


            ????????
            public ? double ?C?
            ????????
            {
            ????????????
            get ?
            ????????????
            {
            ????????????????
            return ?iC;
            ????????????}

            ????????}

            ????}


            ????
            class ?caretaker?
            ????
            {
            ????????
            ????}


            ????????
            /// ? <summary>
            ????
            /// ????Summary?description?for?Client.
            ????
            /// ? </summary>

            ???? public ? class ?Client
            ????
            {
            ????????
            public ? static ? int ?Main( string []?args)
            ????????
            {??????????????
            ????????????Originator?o?
            = ? new ?Originator();
            ????????????
            ????????????
            // ?Assume?that?during?the?course?of?running?an?application?
            ????????????
            // ?we?we?set?various?data?in?the?originator
            ????????????o.MakeSale( 45.0 );
            ????????????o.MakeSale(
            60.0 );

            ????????????
            // ?Now?we?wish?to?record?the?state?of?the?object
            ????????????Memento?m? = ?o.CreateMemento();

            ????????????
            // ?We?make?further?changes?to?the?object
            ????????????o.MakeSale( 60.0 );
            ????????????o.MakeSale(
            10.0 );
            ????????????o.MakeSale(
            320.0 );

            ????????????
            // ?Then?we?decide?ot?change?our?minds,?and?revert?to?the?saved?state?(and?lose?the?changes?since?then)
            ????????????o.SetMemento(m);

            ????????????
            return ? 0 ;
            ????????}

            ????}

            }


            // ?Memento?pattern?--?Real?World?example??


            using ?System;

            namespace ?DoFactory.GangOfFour.Memento.RealWorld
            {

            ??
            // ?MainApp?test?application?

            ??
            class ?MainApp
            ??
            {
            ????
            static ? void ?Main()
            ????
            {
            ??????SalesProspect?s?
            = ? new ?SalesProspect();
            ??????s.Name?
            = ? " Noel?van?Halen " ;
            ??????s.Phone?
            = ? " (412)?256-0990 " ;
            ??????s.Budget?
            = ? 25000.0 ;

            ??????
            // ?Store?internal?state?
            ??????ProspectMemory?m? = ? new ?ProspectMemory();
            ??????m.Memento?
            = ?s.SaveMemento();

            ??????
            // ?Continue?changing?originator?
            ??????s.Name? = ? " Leo?Welch " ;
            ??????s.Phone?
            = ? " (310)?209-7111 " ;
            ??????s.Budget?
            = ? 1000000.0 ;

            ??????
            // ?Restore?saved?state?
            ??????s.RestoreMemento(m.Memento);

            ??????
            // ?Wait?for?user?
            ??????Console.Read();
            ????}

            ??}


            ??
            // ?"Originator"?

            ??
            class ?SalesProspect
            ??
            {
            ????
            private ? string ?name;
            ????
            private ? string ?phone;
            ????
            private ? double ?budget;

            ????
            // ?Properties?
            ???? public ? string ?Name
            ????
            {
            ??????
            get {? return ?name;?}
            ??????
            set
            ??????
            {?
            ????????name?
            = ?value;?
            ????????Console.WriteLine(
            " Name:? " ? + ?name);
            ??????}

            ????}


            ????
            public ? string ?Phone
            ????
            {
            ??????
            get {? return ?phone;?}
            ??????
            set
            ??????
            {?
            ????????phone?
            = ?value;?
            ????????Console.WriteLine(
            " Phone:? " ? + ?phone);
            ??????}

            ????}


            ????
            public ? double ?Budget
            ????
            {
            ??????
            get {? return ?budget;?}
            ??????
            set
            ??????
            {?
            ????????budget?
            = ?value;?
            ????????Console.WriteLine(
            " Budget:? " ? + ?budget);
            ??????}

            ????}


            ????
            public ?Memento?SaveMemento()
            ????
            {
            ??????Console.WriteLine(
            " \nSaving?state?--\n " );
            ??????
            return ? new ?Memento(name,?phone,?budget);
            ????}


            ????
            public ? void ?RestoreMemento(Memento?memento)
            ????
            {
            ??????Console.WriteLine(
            " \nRestoring?state?--\n " );
            ??????
            this .Name? = ?memento.Name;
            ??????
            this .Phone? = ?memento.Phone;
            ??????
            this .Budget? = ?memento.Budget;
            ????}

            ??}


            ??
            // ?"Memento"?

            ??
            class ?Memento
            ??
            {
            ????
            private ? string ?name;
            ????
            private ? string ?phone;
            ????
            private ? double ?budget;

            ????
            // ?Constructor?
            ???? public ?Memento( string ?name,? string ?phone,? double ?budget)
            ????
            {
            ??????
            this .name? = ?name;
            ??????
            this .phone? = ?phone;
            ??????
            this .budget? = ?budget;
            ????}


            ????
            // ?Properties?
            ???? public ? string ?Name
            ????
            {
            ??????
            get {? return ?name;?}
            ??????
            set {?name? = ?value;?}
            ????}


            ????
            public ? string ?Phone
            ????
            {
            ??????
            get {? return ?phone;?}
            ??????
            set {?phone? = ?value;?}
            ????}


            ????
            public ? double ?Budget
            ????
            {
            ??????
            get {? return ?budget;?}
            ??????
            set {?budget? = ?value;?}
            ????}

            ??}


            ??
            // ?"Caretaker"?

            ??
            class ?ProspectMemory
            ??
            {
            ????
            private ?Memento?memento;

            ????
            // ?Property?
            ???? public ?Memento?Memento
            ????
            {
            ??????
            set {?memento? = ?value;?}
            ??????
            get {? return ?memento;?}
            ????}

            ??}

            }

            ?
            Output
            Name:?? Noel van Halen
            Phone:? (412) 256-0990
            Budget: 25000

            Saving state --

            Name:?? Leo Welch
            Phone:? (310) 209-7111
            Budget: 1000000

            Restoring state --

            Name:?? Noel van Halen
            Phone:? (412) 256-0990
            Budget: 25000

            posted on 2006-01-03 16:12 夢在天涯 閱讀(766) 評論(1)  編輯 收藏 引用 所屬分類: Design pattern

            評論

            # re: 模式設計c#--行為型--menento 2006-04-25 09:23 夢在天涯

            備忘錄模式還好理解拉!哈哈  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評論

            閱讀排行榜

            少妇人妻综合久久中文字幕| 久久免费国产精品一区二区| 色综合久久88色综合天天| 久久妇女高潮几次MBA| 久久久WWW成人免费精品| 日韩精品久久久久久| 久久美女人爽女人爽| 久久99精品久久久久子伦| 国内精品伊人久久久久AV影院| 亚洲第一极品精品无码久久| 一本久道久久综合狠狠躁AV| 亚洲精品NV久久久久久久久久 | 亚洲国产成人精品91久久久 | 中文精品久久久久人妻不卡| 四虎国产精品成人免费久久| 一级女性全黄久久生活片免费| 一本大道久久香蕉成人网| 综合久久国产九一剧情麻豆| 久久久久人妻精品一区 | 久久精品无码午夜福利理论片| 亚洲va久久久噜噜噜久久狠狠 | 99久久婷婷国产综合精品草原| 国产精品青草久久久久婷婷 | 91精品国产高清久久久久久国产嫩草| 国产精品一久久香蕉国产线看观看| 国产Av激情久久无码天堂| 青青草原综合久久大伊人精品| 久久综合色之久久综合| 狠狠色婷婷久久综合频道日韩| 精品久久久久中文字幕日本| 久久WWW免费人成—看片| 亚洲色欲久久久综合网东京热| 久久被窝电影亚洲爽爽爽| 色欲综合久久躁天天躁| 97久久久精品综合88久久| 亚洲国产成人久久笫一页| 久久A级毛片免费观看| 一本久久综合亚洲鲁鲁五月天| 久久99精品久久久久久久久久| 久久人人爽人人澡人人高潮AV | 亚洲AⅤ优女AV综合久久久|