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

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            国内精品久久久久影院网站 | 久久www免费人成看国产片| 欧美精品一区二区久久| 精品久久久久成人码免费动漫 | 亚洲va久久久噜噜噜久久天堂| 国产亚洲精久久久久久无码77777| 蜜桃麻豆www久久国产精品| 久久免费精品视频| 久久e热在这里只有国产中文精品99| 久久99国产亚洲高清观看首页 | 精品综合久久久久久88小说| 久久婷婷久久一区二区三区| 亚洲午夜久久久精品影院| 欧美性大战久久久久久| 伊人久久大香线蕉av不变影院 | 无码人妻久久一区二区三区蜜桃| 狠狠精品久久久无码中文字幕 | 久久综合久久综合亚洲| 国产精品青草久久久久婷婷| 国内精品伊人久久久影院| 亚洲国产视频久久| 久久国产亚洲高清观看| 久久久久亚洲国产| 午夜天堂精品久久久久| 久久九九全国免费| 亚洲Av无码国产情品久久| 中文无码久久精品| 久久精品国产一区二区电影| 久久亚洲综合色一区二区三区| 国产亚洲美女精品久久久| 综合久久精品色| 日产久久强奸免费的看| 97精品依人久久久大香线蕉97| 国产成人精品久久免费动漫| 欧美久久一级内射wwwwww.| 无码AV中文字幕久久专区| 国产午夜精品久久久久九九电影| 亚洲精品国产第一综合99久久| 久久99精品国产麻豆宅宅| 中文字幕无码久久精品青草| 一本久久久久久久|