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

            積木

            No sub title

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉(zhuǎn)載自:http://patmusing.blog.163.com/blog/static/13583496020101501825958/


            Aka. Token

            在不破壞封裝性的前提下,捕獲一個對象的內(nèi)部狀態(tài),并在該對象之外保存其狀態(tài)這樣以后就可以將該對象恢復到原先保存的狀態(tài)。

            “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.” – GoF

            動機

            有時候需要記錄一個對象的內(nèi)部狀態(tài)。比如要實現(xiàn)checkpoint或者undo這樣的機制,可以讓使用者從臨時性的操作跳出來或者需要修復錯誤的時候,你必須將狀態(tài)信息保存在某個地方,以便在進行某些操作后,將對象恢復到原來的狀態(tài)。但通常情況下,對象封裝了狀態(tài)(即私有成員變量),因此其他的對象無法訪問這些狀態(tài),而且也不可能將這些狀態(tài)保存在對象之外。如果將這些狀態(tài)設置成公有的,又會違反面向?qū)ο蠓庋b性的原則,同時也會削弱應用的可靠性和可擴展性。

            在軟件構建過程中, 某些對象的狀態(tài)在轉(zhuǎn)換過程中,可能由于某種需要,要求程序能夠回溯到對象之前處于某個時刻的狀態(tài)。如果使用一些共有接口來讓其他對象得到對象的狀態(tài),便會暴露對象的細節(jié)實現(xiàn)。Memento設計模式就可以實現(xiàn)對象狀態(tài)的良好保存與恢復,但同時又不會因此而破壞對象本身的封裝性。

            UML類圖:

            21. C++實現(xiàn)Behavioral - Memento模式 - 玄機逸士 - 玄機逸士博客

            角色

            - Memento

            1. 保存Originator對象的內(nèi)部狀態(tài)。

            2. Originator外,其他對象均不能訪問Memento對象。

            - Originator

            1. 創(chuàng)建一個包含其當前內(nèi)部狀態(tài)快照的Memento對象。

            2. 使用Memento對象來恢復其內(nèi)部狀態(tài)。

            - Caretaker

            1. Memento對象的容器。在C++中一般用stack來實現(xiàn)。

            2. 從不對Memento對象的內(nèi)容進行檢查或操作。

            示例代碼:

            // Memento.h

            #include <iostream>

            #include <stack>

            using namespace std;

            // CMemento類,用來保存CRectangle的狀態(tài)

            class CMemento

            {

            private: // 所有的成員變量和成員函數(shù)都是私有的

            int topx; // 因此除友元類CRectangle外,其他對象都無法訪問

            int topy;

            int width;

            int height;

            private:

            CMemento()

            {

            }

            //private: // 1. 如果編寫了顯式拷貝構造函數(shù),那么,它必須是公有的,

            // CMemento(const CMemento& memo) // 否則CMementoStack將無法調(diào)用該拷貝構造函數(shù)。

            // { // 2. 如果沒有顯式的拷貝構造函數(shù),那么缺省的拷貝構造函數(shù)總是公有的。

            // topx = memo.topx; // 3. Memento模式中,如果僅考慮保存一次狀態(tài),則

            // topy = memo.topy; // CMementoStack是不必要的,那么拷貝構造函數(shù),可以

            // width = memo.width; // 顯式地聲明為private的,盡管在CRectanglecreate_memento

            // height = memo.height; // 成員函數(shù)中也會調(diào)用CMemento的拷貝構造函數(shù),但CRectangle

            // } // CMemento的友元類,因此不存在這方面的限制。

            private:

            void set_state(int topx, int topy, int width, int height) // 保存CRectangle的狀態(tài)

            {

            this->topx = topx;

            this->topy = topy;

            this->width = width;

            this->height = height;

            }

            friend class CRectangle; // 友元類CRectangle,可以訪問CMemento中的所有內(nèi)容

            };

            // CRectangle類。一個矩形,需要保存狀態(tài)改變的類

            class CRectangle

            {

            private:

            int topx; // 矩形左上角的x坐標

            int topy; // 矩形左上角的y坐標

            int width; // 矩形的寬

            int height; // 矩形的高

            public:

            CRectangle(int topx, int topy, int width, int height):topx(topx), topy(topy), width(width), height(height)

            {

            }

            // 模擬移動矩形的位置到指定的點,即改變了矩形的狀態(tài)

            void move_to(int topx, int topy)

            {

            this->topx = topx;

            this->topy = topy;

            }

            // 模擬改變矩形的長和寬,即改變了矩形的狀態(tài)

            void change_width_height(int width, int height)

            {

            this->width = width;

            this->height = height;

            }

            // 將矩形恢復到memo中所保存的狀態(tài)

            void set_memento(CMemento memo)

            {

            this->topx = memo.topx;

            this->topy = memo.topy;

            this->width = memo.width;

            this->height = memo.height;

            }

            // 將矩形的狀態(tài)保存到一個CMemento對象

            CMemento create_memento()

            {

            CMemento cm;

            cm.set_state(this->topx, this->topy, this->width, this->height);

            return cm;

            }

            // 輸出矩形的狀態(tài)信息

            void print_info()

            {

            cout << "Top left point's x coordinate: " << topx << endl;

            cout << "Top left point's y coordinate: " << topy << endl;

            cout << "The width is: " << width << endl;

            cout << "The height is: " << height << endl;

            }

            };

            // CMemento對象的容器,可以用來保存多個CMemento對象,通常用stack來實現(xiàn)

            class CMementoStack

            {

            private:

            stack<CMemento> stk;

            public:

            void add_memento(CMemento memo)

            {

            stk.push(memo); //CMemento對象壓入棧中

            }

            CMemento get_memento()

            {

            CMemento cm = stk.top(); // 取得CMemento對象。這個過程會用到CMemento類的拷貝構造函數(shù),

            // 由于CMemento對象中的成員變量均是普通類型(非指針、非類對象)

            // 因此使用默認的拷貝構造函數(shù)即可

            stk.pop(); // 刪除已經(jīng)取得的CMemento對象

            return cm;

            }

            };

            // Memento.cpp

            #include "Memento.h"

            int main(int argc, char **argv)

            {

            CRectangle cr(10, 10, 100, 100);

            CMementoStack cs;

            cout << "Initial states: " << endl;

            cr.print_info();

            CMemento cm0 = cr.create_memento(); // 將狀態(tài)保存到CMemento對象

            cs.add_memento(cm0); // CMemento對象壓棧

            // 第一次改變狀態(tài)

            cr.change_width_height(200, 200); // 改變矩形的高度和寬度

            cr.move_to(20, 20); // 改變矩形的位置

            cout << "\nAfter 1st states changed: " << endl;

            cr.print_info();

            CMemento cm1 = cr.create_memento(); // 將狀態(tài)保存到CMemento對象

            cs.add_memento(cm1); // CMemento對象壓棧

            // 第二次改變狀態(tài)

            cr.change_width_height(300, 300); // 改變矩形的高度和寬度

            cr.move_to(30, 30); // 改變矩形的位置

            cout << "\nAfter 2nd states changed: " << endl;

            cr.print_info();

            // ... 這里不再壓棧

            // 恢復到第一次狀態(tài)的改變

            cr.set_memento(cs.get_memento());

            cout << "\nStates restored to 1st change: " << endl;

            cr.print_info();

            // 恢復到初始狀態(tài)

            cr.set_memento(cs.get_memento());

            cout << "\nStates restored to initial: " << endl;

            cr.print_info();

            }

            運行結果:

            Initial states:

            Top left point's x coordinate: 10

            Top left point's y coordinate: 10

            The width is: 100

            The height is: 100

            After 1st states changed:

            Top left point's x coordinate: 20

            Top left point's y coordinate: 20

            The width is: 200

            The height is: 200

            After 2nd states changed:

            Top left point's x coordinate: 30

            Top left point's y coordinate: 30

            The width is: 300

            The height is: 300

            States restored to 1st change:

            Top left point's x coordinate: 20

            Top left point's y coordinate: 20

            The width is: 200

            The height is: 200

            States restored to initial:

            Top left point's x coordinate: 10

            Top left point's y coordinate: 10

            The width is: 100

            The height is: 100

            結果符合預期。

            補充說明:

            1. 未設置其成員變量為public的前提下,在對象的外部保存一個其狀態(tài),頗需技巧,而且用不同語言來實現(xiàn)的時候也有所不同。

            a. 對于C++,通常使用友元類來實現(xiàn)。

            b. 對于C#,使用internal關鍵字。

            c. 對于Java,使用package protected訪問控制。

            與其處于相同包中的子類,和處于相同包中的其它類均可以訪問pakcage protected的對象或變量。

            Java中的訪問權限有public,private,protected和默認的包訪問權限,如果類中的屬性方法沒有顯示的指明訪問權

            限,則具有包訪問權限,很多人也稱它為friendly訪問權限,也有人稱為packeged權限,而packagedfriendly

            這兩個關鍵字在實際中都是不存在的。在Java中,訪問權限修飾符權限從高到低是publicprotectedpackage

            protectedprivate

            d. C++, C#Java均可使用內(nèi)部類的方式來實現(xiàn)類似的功能,不過對于“將狀態(tài)存儲于對象之外”而言,稍嫌勉強。

            2. 關于拷貝構造函數(shù),請看:http://patmusing.blog.163.com/blog/static/1358349602009113061024796/

            3. 關于友元類,請看:http://patmusing.blog.163.com/blog/static/1358349602010182331153/



            posted on 2013-03-08 14:51 Jacc.Kim 閱讀(210) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            狠狠色丁香婷婷综合久久来| 91精品国产高清久久久久久io| 久久久久香蕉视频| 久久久久国产日韩精品网站| 亚洲狠狠综合久久| 日日狠狠久久偷偷色综合免费| 久久九九久精品国产免费直播| 三级三级久久三级久久| 少妇久久久久久久久久| 国产一区二区精品久久岳| 狠狠色丁香久久婷婷综合_中| 久久无码人妻一区二区三区| 国产三级精品久久| 久久精品无码一区二区无码 | 久久久久青草线蕉综合超碰| 精品综合久久久久久888蜜芽| 久久国产免费直播| 亚洲成人精品久久| 久久91综合国产91久久精品| 日本精品久久久久久久久免费| 99久久99这里只有免费的精品| 色诱久久av| 久久久久香蕉视频| 91久久精品国产免费直播| 97超级碰碰碰碰久久久久| 久久精品国产亚洲av麻豆图片| 久久久国产精品网站| 97精品伊人久久大香线蕉app| 性高湖久久久久久久久| 久久亚洲精精品中文字幕| 国产成年无码久久久久毛片| 国产成人综合久久久久久| 久久精品国产亚洲AV久| 久久99国产精品二区不卡| AA级片免费看视频久久| 性欧美大战久久久久久久久| 91超碰碰碰碰久久久久久综合| 亚洲国产精品久久| 久久久久人妻精品一区二区三区| 国产情侣久久久久aⅴ免费| 久久久久四虎国产精品|