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

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            設計模式之factory工廠模式(三):Abstract Factory抽象工廠模式

            Abstract Factory抽象工廠模式實際上只是使用多個Factory Method。每個Factory Method創建一個不同類型的對象。下面的例子代碼假設要創建一個通用的游戲環境,并支持不同類型的游戲。
            #include <iostream>
            using namspace std;

            class Obstacle{
             public:
               virtural void action()=0;
            };//抽象基類

            class Player{
            public:
               virtural void interactWith(Obstacle*)=0;
            };//抽象基類

            class Kitty:public Player{
               virtual void interactWith(Obstacle*){
               cout<<"KungFuGuy now battles against a";
               ob->action();
            };

            class Puzzile:public Obstacle{
            public:
               void action(){
                  cout << "Puzzle" << endl;}
            };

            class NastyWeapon:public Obstacle{
            public:
               void action(){
                  cout << "NastyWeapon" << endl;}
            }; 

            //The abstract factory
            class GameElementFactory{
            public:
               virtual Player* makePlayer()=0;
               virtual Obstacle* makeObstacle()=0;
            };

            //Concreae factories
            class KittiesAndPuzzles:public GameElementFactory{
            public:
               virtual Player* makePlayer() {return new Kitty;}
               virtual Obstacle* makeObstacle() {return new Puzzle;}
            };

            class KillAndDismember:public GameElementFactory{
            public:
               virtual Player* makePlayer() {return new KungFuGuy;}
               virtual Obstacle* makeObstacle() {return new NastyWeapon;}
            };

            class GameEnvironment{
               GameElementFactory* gef;
               Player* p;
               Obstacle* ob;
            public:
               GameEnvironment(GameElementFactory* factory):get(factory),p(factory->makePlayer()),ob(factory->makeObstacle()){}
            void play() {p->interacWith(ob);}
            ~GameEnvrionment(){
               delete p;
               delete ob;
               delete gef;
               }
            };

            int main(){
               GameEnvironment g1(new KittiesAndPuzzles),g2(new KillAndDismember);
               g1.play();
               g2.play();
            }

            /* Output:
            Kitty has encountered a Puzzle
            KungFuGuy now battles against a NastyWeapon *///

            posted on 2009-04-14 22:04 Benjamin 閱讀(407) 評論(1)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: 設計模式之factory工廠模式(三):Abstract Factory抽象工廠模式  回復  更多評論   

            //這兩個class要修正一下:
            class Kitty:public Player{
            virtual void interactWith(Obstacle* ob){
            cout<< "Kitty ha encountered a ";
            ob->action();
            }
            };
            class KungFuGuy:public Player{
            virtual void interactWith(Obstacle* ob){
            cout <<"KungFuGuy now battles against a ";
            ob->action();
            }
            };
            2009-09-04 21:05 | 李熙建
            久久人人爽人人爽人人片AV东京热 | 国产精品天天影视久久综合网| 久久精品国产99久久久香蕉| 久久久久久久久久免免费精品| 国产精品日韩深夜福利久久| 欧美性猛交xxxx免费看久久久| 久久大香香蕉国产| 久久久久无码中| 久久国产精品免费一区| 亚洲AV无码久久精品蜜桃| 久久se这里只有精品| 99久久精品免费| 国产成人精品免费久久久久| 久久91精品国产91久| 亚洲欧美日韩精品久久亚洲区| 99久久综合狠狠综合久久| 熟妇人妻久久中文字幕| 日本精品久久久久影院日本| 日本国产精品久久| 久久久噜噜噜久久中文字幕色伊伊| 色婷婷久久综合中文久久一本| 国产精品午夜久久| 久久久青草青青国产亚洲免观| 欧美精品丝袜久久久中文字幕| 色悠久久久久久久综合网| 久久久久国产精品嫩草影院| 国产精品久久久久乳精品爆 | 欧美激情精品久久久久久| 久久久这里有精品| 久久av免费天堂小草播放| 久久久久久免费视频| 国产精品18久久久久久vr | 久久久噜噜噜www成人网| 精品久久久久久无码免费| 噜噜噜色噜噜噜久久| 久久国产高清一区二区三区| 国产精品久久久久久| 午夜久久久久久禁播电影| 日本WV一本一道久久香蕉| 亚洲天堂久久精品| 国产日产久久高清欧美一区|