• <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>
            posts - 183,  comments - 10,  trackbacks - 0

            來自于《大話設(shè)計(jì)模式》
            訪問者模式(Visitor):
            表示一個(gè)作用于某對(duì)象結(jié)構(gòu)中的個(gè)元素的操作。它是你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。

            行為型

            UML 類圖:



            代碼實(shí)現(xiàn) C++:
              1 #include <iostream>
              2 #include <list>
              3 #include <algorithm>
              4 #include <string>
              5 using namespace std;
              6 
              7 class Action;
              8 
              9 class Person
             10 {
             11 protected:
             12     string name;
             13 public:
             14     Person(const string& s = "Person") : name(s) {}
             15     virtual void Accept(Action* visitor) = 0;
             16     virtual string getName()
             17     {
             18         return name;
             19     }
             20 };
             21 
             22 class Man;
             23 class Woman;
             24 
             25 class Action
             26 {
             27 protected:
             28     string name;
             29 public:
             30     Action(const string& s = "Action") : name(s) {}
             31     virtual void GetManConclusion(Man* m) = 0;
             32     virtual void GetWomanConclusion(Woman* w) = 0;
             33 };
             34 
             35 class Man : public Person
             36 {
             37 public:
             38     Man(const string& s = "Man") : Person(s) {}
             39     virtual void Accept(Action* visitor)
             40     {
             41         visitor->GetManConclusion(this);
             42     }
             43 };
             44 
             45 class Woman : public Person
             46 {
             47 public:
             48     Woman(const string& s = "Woman") : Person(s) {}
             49     virtual void Accept(Action* visitor)
             50     {
             51         visitor->GetWomanConclusion(this);
             52     }
             53 };
             54 
             55 class Success : public Action
             56 {
             57 public:
             58     Success(const string& s = "Success") : Action(s) {}
             59     virtual void GetManConclusion(Man* m)
             60     {
             61         cout << name << endl;
             62         cout << m->getName() << endl;
             63         cout << "1" << endl;
             64     }
             65     virtual void GetWomanConclusion(Woman* w)
             66     {
             67         cout << name << endl;
             68         cout << w->getName() << endl;
             69         cout << "2" << endl;
             70     }
             71 };
             72 
             73 class Failing : public Action
             74 {
             75 public:
             76     Failing(const string& s = "Failing") : Action(s) {}
             77     virtual void GetManConclusion(Man* m)
             78     {
             79         cout << name << endl;
             80         cout << m->getName() << endl;
             81         cout << "3" << endl;
             82     }
             83     virtual void GetWomanConclusion(Woman* w)
             84     {
             85         cout << name << endl;
             86         cout << w->getName() << endl;
             87         cout << "4" << endl;
             88     }
             89 };
             90 
             91 class Amativeness : public Action
             92 {
             93 public:
             94     Amativeness(const string& s = "Amativeness") : Action(s) {}
             95     virtual void GetManConclusion(Man* m)
             96     {
             97         cout << name << endl;
             98         cout << m->getName() << endl;
             99         cout << "5" << endl;
            100     }
            101     virtual void GetWomanConclusion(Woman* w)
            102     {
            103         cout << name << endl;
            104         cout << w->getName() << endl;
            105         cout << "6" << endl;
            106     }
            107 };
            108 
            109 class Marriage : public Action
            110 {
            111 public:
            112     Marriage(const string& s = "Marriage") : Action(s) {}
            113     virtual void GetManConclusion(Man* m)
            114     {
            115         cout << name << endl;
            116         cout << m->getName() << endl;
            117         cout << "7" << endl;
            118     }
            119     virtual void GetWomanConclusion(Woman* w)
            120     {
            121         cout << name << endl;
            122         cout << w->getName() << endl;
            123         cout << "8" << endl;
            124     }
            125 };
            126 
            127 class ObjectStructure
            128 {
            129 private:
            130     list<Person*> elements;
            131 public:
            132     ObjectStructure() {}
            133     ~ObjectStructure()
            134     {
            135         for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
            136         {
            137             delete (*iter);
            138         }
            139     }
            140     void Attach(Person* element)
            141     {
            142         elements.push_back(element);
            143     }
            144     void Detach(Person* element)
            145     {
            146         list<Person*>::iterator iter = find(elements.begin(), elements.end(), element);
            147         if (iter != elements.end())
            148         {
            149             elements.erase(iter);
            150         }
            151     }
            152     void Display(Action* visitor)
            153     {
            154         for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
            155         {
            156             (*iter)->Accept(visitor);
            157         }
            158     }
            159     int size()
            160     {
            161         return elements.size();
            162     }
            163 };
            164 
            165 int main()
            166 {
            167     ObjectStructure o;
            168     o.Attach(new Man);
            169     o.Attach(new Woman);
            170 
            171     cout << o.size() << endl;
            172 
            173     Success* v1 = new Success;
            174     o.Display(v1);
            175     Failing* v2 = new Failing;
            176     o.Display(v2);
            177     Amativeness* v3 = new Amativeness;
            178     o.Display(v3);
            179     Marriage* v4 = new Marriage;
            180     o.Display(v4);
            181 
            182     delete v4;
            183     delete v3;
            184     delete v2;
            185     delete v1;
            186 
            187     return 0;
            188 }
            posted on 2011-04-30 15:21 unixfy 閱讀(457) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲中文字幕无码久久2020| 久久伊人五月天论坛| 无码人妻久久一区二区三区免费 | 久久青草国产精品一区| 久久狠狠色狠狠色综合| 一本色综合久久| 99久久久精品免费观看国产| 一本久道久久综合狠狠躁AV| 久久精品国产亚洲av水果派| 日本精品久久久久影院日本| 久久精品欧美日韩精品| 久久Av无码精品人妻系列| 久久99精品久久久久久噜噜| 久久人人爽人人爽人人片AV东京热| 国内精品久久久久久99蜜桃| 色综合久久中文字幕综合网| 久久国产精品77777| 久久精品综合网| 国产激情久久久久影院老熟女免费| 久久久久亚洲av成人网人人软件| 国产精品视频久久久| 亚洲精品乱码久久久久久自慰 | 久久久久久毛片免费看| 午夜天堂av天堂久久久| 久久人人爽人人爽人人片AV东京热| 亚洲va国产va天堂va久久| 性做久久久久久久久浪潮| 久久精品成人免费国产片小草| 国产精品9999久久久久| 久久综合九色综合网站| 合区精品久久久中文字幕一区| 九九久久精品国产| 亚洲国产精品久久久久久| 久久精品国产一区| 久久久91精品国产一区二区三区| 久久精品国产亚洲av日韩| 久久精品国产99久久无毒不卡| 国内精品久久久久久久97牛牛| 久久国产色AV免费观看| 久久精品国产亚洲网站| 伊人久久大香线蕉影院95|