• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150249596/

            表示一個作用于某對象結構中的各元素的操作。它可以在不改變各元素的類的前提下定義作用于這些元素的新的操作。

            “Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.” – GoF

            類層次結構中可能經常由于引入新的操作,從而將類型變得很脆弱。比如假定三個類RectangleTriangleLine繼承自抽象類Shape,現在由于某種原因需要在抽象類Shape中增加一個abstract (或者pure virtual)方法,如果使用通常的方法,那么它所有的子類,即RectangleTriangleLine都需要修改。

            26. C++實現Behavioral - Visitor模式 - 玄機逸士 - 玄機逸士博客

            由于需求的改變,某些類層次結構中常常需要增加新的行為(方法),如果直接在基類中做這樣的更改,將會給子類帶來很繁重的變更負擔,甚至破壞原有的設計。Visitor設計模式就是在不更改類層次結構的前提下,在運行時根據需要動態地為類層次結構上的各個類動態添加新的操作(方法)

            下面是Visitor模式的UML類圖:

            26. C++實現Behavioral - Visitor模式 - 玄機逸士 - 玄機逸士博客

            Visitor設計模式要解決的問題:

            - 類層次結構不變,但類的方法需要增加;

            - 預料到基類有更改,但不知道是何種具體更改。

            要增加一個方法,實際上就是增加一個Visitor的子類。

            業務實例:

            RectangleCircle兩種形狀,在不改動他們代碼的情況下,分別為它們增加填充(Fill)和附加文字(AddText)的功能。

            實現上面業務實例的C++代碼:

            // Visitor.h

            #include <iostream>

            using namespace std;

            class Visitor;

            class Shape

            {

            public:

            // draw為已經確知需要的方法

            virtual void draw() = 0;

            // 1. 預料將來可能會引入新的方法,因此在這里預留一個accept方法

            // 2. 可以通過下面的accept方法,為Shape的子類增加一個或多個方法

            virtual void accept(Visitor *visitor) = 0;

            public:

            virtual ~Shape()

            {

            cout << "in the destructor of Shape..." << endl;

            }

            };

            class Rectangle : public Shape

            {

            public:

            void draw();

            void accept(Visitor *visitor);

            public:

            ~Rectangle()

            {

            cout << "in the destructor of Rectangle..." << endl;

            }

            };

            class Circle : public Shape

            {

            public:

            void draw();

            void accept(Visitor *visitor);

            public:

            ~Circle()

            {

            cout << "in the destructor of Circle..." << endl;

            }

            };

            class Visitor

            {

            public:

            // 注意此處參數為具體類,而非抽象類Shape

            // Shape有多少個子類,就需要在此處重載多少次visit方法。

            // 除非使用RTTI,則可以只有一個visit方法,并且該方法的參數類型為抽象的Shape*,同時需要在visit方法中確定,

            // 傳入的參數到底是Shape的哪個具體的子類。

            virtual void visit(Rectangle *shape) = 0;

            virtual void visit(Circle *shape) = 0;

            public:

            virtual ~Visitor()

            {

            cout << "in the destructor of Visitor..." << endl;

            }

            };

            // 假定要為Shape的子類增加一個填充(Fill)操作,那么就為Visitor增加一個子類,即FillVisitor

            class FillVisitor : public Visitor

            {

            public:

            void visit(Rectangle *shape)

            {

            // 為類Rectangle增加一個方法,由于實現這個新方法可能需要用到原來對象中的屬性或者方法,因此需要

            // 將自身(this)傳到這里。理論上而言,如果不用到原來對象的原有的屬性或者方法,那么visit方法是可以

            // 沒有參數的;另外一種情況就是,如果必要,visit方法也可以擁有多個參數

            cout << "Here is the newly added Fill() method for class Rectangle." << endl;

            }

            void visit(Circle *shape)

            {

            // 為類Circle增加一個方法,由于實現這個新方法可能需要用到原來對象中的屬性或者方法,因此需要

            // 將自身(this)傳到這里。理論上而言,如果不用到原來對象的原有的屬性或者方法,那么visit方法是可以

            // 沒有參數的;另外一種情況就是,如果必要,visit方法也可以擁有多個參數

            cout << "Here is the newly added Fill() method for class Circle." << endl;

            }

            public:

            ~FillVisitor()

            {

            cout << "in the destructor of FillVisitor..." << endl;

            }

            };

            // 假定要為Shape的子類增加一個附加文字(AddText)操作,那么就為Visitor增加一個子類,即AddTextVisitor

            class AddTextVisitor : public Visitor

            {

            public:

            void visit(Rectangle *shape)

            {

            // 為類Rectangle增加一個方法,由于實現這個新方法可能需要用到原來對象中的屬性或者方法,因此需要

            // 將自身(this)傳到這里。理論上而言,如果不用到原來對象的原有的屬性或者方法,那么visit方法是可以

            // 沒有參數的;另外一種情況就是,如果必要,visit方法也可以擁有多個參數

            cout << "Here is the newly added AddText() method for class Rectangle." << endl;

            }

            void visit(Circle *shape)

            {

            // 為類Circle增加一個方法,由于實現這個新方法可能需要用到原來對象中的屬性或者方法,因此需要

            // 將自身(this)傳到這里。理論上而言,如果不用到原來對象的原有的屬性或者方法,那么visit方法是可以

            // 沒有參數的;另外一種情況就是,如果必要,visit方法也可以擁有多個參數

            cout << "Here is the newly added AddText() method for class Circle." << endl;

            }

            public:

            ~AddTextVisitor()

            {

            cout << "in the destructor of AddTextVisitor..." << endl;

            }

            };

            // Visitor.cpp

            #include "Visitor.h"

            void Rectangle::draw()

            {

            cout << "Implement method Rectangle::draw() here!" << endl;

            }

            void Rectangle::accept(Visitor *visitor)

            {

            visitor->visit(this); // 轉發到visitorvisit方法,并將this作為參數傳遞過去

            }

            void Circle::draw()

            {

            cout << "Implement method Circle::draw() here!" << endl;

            }

            void Circle::accept(Visitor *visitor)

            {

            visitor->visit(this); // 轉發到visitorvisit方法,并將this作為參數傳遞過去

            }

            // PatternClient.cpp

            #include "Visitor.h"

            int main(int argc, char **argv)

            {

            Visitor *visitor1 = new FillVisitor();

            Shape *rectangle = new Rectangle();

            // 調用該形狀已經實現的方法

            rectangle->draw();

            // 通過visitor1調用新增加的方法

            rectangle->accept(visitor1);

            Shape *circle = new Circle();

            // 調用該形狀已經實現的方法

            rectangle->draw();

            // 通過visitor1調用新增加的方法

            circle->accept(visitor1);

            cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

            Visitor *visitor2 = new AddTextVisitor();

            // 通過visitor2調用新增加的方法

            rectangle->accept(visitor2);

            // 通過visitor2調用新增加的方法

            circle->accept(visitor2);

            cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

            delete visitor1;

            delete visitor2;

            delete rectangle;

            delete circle;

            return 0;

            }

            運行結果:

            Implement method Rectangle::draw() here!

            Here is the newly added Fill() method for class Rectangle.

            Implement method Rectangle::draw() here!

            Here is the newly added Fill() method for class Circle.

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            Here is the newly added AddText() method for class Rectangle.

            Here is the newly added AddText() method for class Circle.

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            in the destructor of FillVisitor...

            in the destructor of Visitor...

            in the destructor of AddTextVisitor...

            in the destructor of Visitor...

            in the destructor of Rectangle...

            in the destructor of Shape...

            in the destructor of Circle...

            in the destructor of Shape...

            結果符合預期。

            由上面的代碼可以很清楚地知道,我們如果要為RectangleCircle增加新方法,只需要多增加一個Visitor的子類即可,而RectangleCircle這兩個類本身的代碼,沒無任何改動。

            上面代碼對應的UML類圖:

            26. C++實現Behavioral - Visitor模式 - 玄機逸士 - 玄機逸士博客

            雙向分發(Double Dispatch)

            - accept(Visitor *visitor)方法在Shape的子類 (Rectangle)中;

            - visit(Rectangle *rectangle)visit方法在Visitor的子類 (FillVisitor)中;

            - 兩處重要的多態特性:

            n 調用accept方法的對象之多態辨析,即是誰調用accept方法?(在本例中,是Rectangle還是Circle)

            n accept方法的參數之多態辨析,即accept方法的參數到底是那個Visitor的子類

            正是這兩處多態特性的使用,就形成了所謂的雙向分發(Double Dispatch)機制,即首先將visitor作為參數傳遞給Shape的子類(在本例中即RectangleCircle);然后在Shape子類的accept方法中調用visitorvisit方法,而且visit方法的參數是this,這樣Shape子類就將自己傳遞到了visitor類的visit方法中,然后在visitorvisit方法中為Shape的子類增加需要的新的行為。這就是雙向分發的核心思想,即visitor被傳遞(分發)了一次,即visitor被傳遞到Shape子類中一次,Shape的子類也被傳遞(分發)了一次,即Shape的子類被傳遞到visitor中一次。

            從更高層次來看這個問題:Shape中的accept()Visitor作為參數,Visitor中的visitShape作為參數。

            上面給出代碼的調用順序:

            26. C++實現Behavioral - Visitor模式 - 玄機逸士 - 玄機逸士博客

            posted on 2013-03-08 16:01 Jacc.Kim 閱讀(253) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 一本色道久久88加勒比—综合| 久久se精品一区精品二区| 午夜视频久久久久一区| 久久综合亚洲欧美成人| 久久www免费人成看国产片| 亚洲AV无一区二区三区久久| 99久久精品无码一区二区毛片| 久久久久国产成人精品亚洲午夜| 人妻精品久久无码专区精东影业| 久久精品亚洲福利| 丁香狠狠色婷婷久久综合| 亚洲欧洲中文日韩久久AV乱码| 国产欧美一区二区久久| 国色天香久久久久久久小说 | 国产AV影片久久久久久| 狠狠色婷婷久久一区二区| 国产亚洲精久久久久久无码AV| 久久精品国产第一区二区三区| 久久精品成人免费观看97| 久久se精品一区二区| 久久精品www人人爽人人| 亚洲人成精品久久久久| 久久人人爽人人爽人人片AV麻烦| 久久精品亚洲乱码伦伦中文| 99久久亚洲综合精品网站| 久久国产高清字幕中文| 国内精品久久国产大陆| 99久久精品午夜一区二区| 亚洲日韩中文无码久久| 久久免费看黄a级毛片| 99精品国产免费久久久久久下载| 久久久久综合中文字幕 | 久久精品视频免费| 久久精品成人国产午夜| 99久久成人18免费网站| 大美女久久久久久j久久| 久久久久久久综合综合狠狠| 欧美久久亚洲精品| 日日噜噜夜夜狠狠久久丁香五月| 色婷婷综合久久久中文字幕|