• <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++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            模式設(shè)計c#--行為型--menento

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


            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 夢在天涯 閱讀(771) 評論(1)  編輯 收藏 引用 所屬分類: Design pattern

            評論

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

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

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲国产精品成人AV无码久久综合影院 | 一级女性全黄久久生活片免费| 99久久国产综合精品女同图片| 久久久久久青草大香综合精品| 情人伊人久久综合亚洲| 久久久国产乱子伦精品作者| 亚洲AV无码久久精品狠狠爱浪潮| 久久婷婷国产剧情内射白浆 | 狼狼综合久久久久综合网| 四虎国产精品成人免费久久| 久久国产综合精品五月天| 久久国产精品免费一区| 亚洲а∨天堂久久精品9966| 久久久国产亚洲精品| 亚洲va久久久噜噜噜久久狠狠 | 91麻精品国产91久久久久| 91精品婷婷国产综合久久| 久久男人中文字幕资源站| 亚洲v国产v天堂a无码久久| 亚洲欧美伊人久久综合一区二区| 日本久久久久亚洲中字幕| 久久精品亚洲日本波多野结衣| 国产精品欧美久久久天天影视| 中文字幕久久欲求不满| 模特私拍国产精品久久| 久久99国内精品自在现线| 久久久久久久久久免免费精品| 99久久做夜夜爱天天做精品| avtt天堂网久久精品| 久久精品国产亚洲Aⅴ香蕉| 亚洲乱码中文字幕久久孕妇黑人| 99精品国产在热久久| 久久99精品久久久久久齐齐| 无码伊人66久久大杳蕉网站谷歌| 亚洲一本综合久久| 91麻豆国产精品91久久久| 97精品伊人久久久大香线蕉| 亚洲午夜久久久久妓女影院| 亚洲一本综合久久| 97久久综合精品久久久综合| 综合久久国产九一剧情麻豆|