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

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

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


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

            評(píng)論

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

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

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807563
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久99国产精品二区不卡| 精品久久久久久国产三级| 亚洲第一永久AV网站久久精品男人的天堂AV| 国产2021久久精品| 久久综合成人网| 久久精品蜜芽亚洲国产AV| 亚洲国产精品人久久| 国内精品伊人久久久影院| 国产精品9999久久久久| 久久天天躁狠狠躁夜夜2020老熟妇| 成人久久免费网站| 久久久无码精品午夜| 亚洲午夜久久久久久噜噜噜| 国产一区二区精品久久凹凸| 亚洲精品乱码久久久久久按摩| 国产午夜福利精品久久| 狠狠精品久久久无码中文字幕| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 99久久99久久| 久久国语露脸国产精品电影| 久久久久久无码国产精品中文字幕| 久久久女人与动物群交毛片| 亚洲欧洲中文日韩久久AV乱码| 99精品久久久久久久婷婷| 久久久亚洲欧洲日产国码二区 | 天天躁日日躁狠狠久久 | 777午夜精品久久av蜜臀| 色婷婷久久综合中文久久一本| 免费观看久久精彩视频| 久久久精品人妻一区二区三区四| 久久久久久久久久久| 久久午夜无码鲁丝片秋霞| 国产福利电影一区二区三区久久久久成人精品综合 | 99久久免费国产精品特黄| 久久久免费观成人影院| 久久久久亚洲精品天堂久久久久久| 久久精品国产亚洲欧美| 久久国产精品-国产精品| 色综合久久中文色婷婷| 97精品伊人久久久大香线蕉| 久久精品国产99久久久香蕉|