• <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

            最新評論

            閱讀排行榜

            思思久久好好热精品国产| 99久久人妻无码精品系列蜜桃| 国产伊人久久| 97精品伊人久久久大香线蕉 | 久久国产精品成人影院| 伊人丁香狠狠色综合久久| 久久久久亚洲精品无码网址| 无遮挡粉嫩小泬久久久久久久 | 久久精品99久久香蕉国产色戒 | 精品久久久久中文字| 精品国产乱码久久久久久人妻| 久久免费高清视频| 久久久久国产精品人妻| 国内精品久久久久久中文字幕| 久久人人爽人人爽人人AV东京热| 久久99精品久久久久久噜噜| 精品无码久久久久久尤物| 免费精品久久天干天干| 久久久中文字幕日本| 久久精品黄AA片一区二区三区| 午夜精品久久久久| 久久天天日天天操综合伊人av| 久久精品国产亚洲AV无码偷窥| 国产精品久久久久蜜芽| 久久成人18免费网站| 日本一区精品久久久久影院| 久久国产亚洲高清观看| 看久久久久久a级毛片| 99久久精品国产一区二区| 久久青青草视频| 久久久久亚洲AV无码去区首| 99热精品久久只有精品| 久久免费高清视频| 亚洲国产成人久久综合一| 久久精品一区二区| 久久精品国产精品国产精品污| 精品久久久久久中文字幕人妻最新| 久久婷婷五月综合国产尤物app| 国产精品久久久香蕉| 久久天天躁狠狠躁夜夜avapp| 中文成人久久久久影院免费观看|