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

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            設(shè)計(jì)模式之工廠(factory)模式(一)

            factory工廠模式是所有模式中最有用的模式之一。它強(qiáng)制用一個(gè)factory工廠來創(chuàng)建對象,程序中所有需創(chuàng)建對象的代碼都在這個(gè)factory中執(zhí)行,它是Factory Method模式的變體。示例代碼:
            #include <iostream>
            #include <stdexcept>
            #include <cstddef>
            #include <string>
            #include <vector>
            #include "../pruge.h"
            using namespace std;

            class Shape{
               virtual void draw()=0;
               virtual void erase()=0;
               virtual ~Shape();
               class BadShapeCreation:public logic_error{
            public:
               BadShapeCreation(string type):logic_error("Cann't create type"+typed){}
            };
               static Shape* factory(const string& type)
               throw(BadShapeCreation);
            };

            class Circle:public Shape{
               Circle(){};//private constructor
               friend class Shape;
            public:
               void draw(){cout<<"Circle::draw"<<endl;}
               void erase(){cout<<"Circle::erase"<<endl;}
               ~Circle(){cout<<"Circle::~Circle" <<endl;}
            };

            class Square:public Shape{
               Square(){};//private constructor
               friend class Shape;
            public:
               void draw(){cout<<"Square::draw"<<endl;}
               void erase(){cout<<"Square::erase"<<endl;}
               ~Square(){cout<<"Square::~Circle" <<endl;}
            };

            Shape* Shape::factory(const string& type)
            throw(Shape::BadShapeCreation){
            if(type== "Circle") return new Circle;
            if(type=="Square") return new Square;
            throw BadShapeCreation(type);
            }

            char* sl[] = {"Circle","Square",Square","Circle","Circle","Circle",Square"};

            int main(){
            vector<Shape*> shape;
            try{
               for(size_t i = 0; i<sizeof(sl)/sizeof(sl[0]); ++i){
                     shape.push_back(shape::factory(sl[i]));
               }catch(Shape::BadShpaeCreation e){
               cout << e.what()<<endl;
               purge(shapes);
               return EXIT_FAILURE;
               }
            for(size_t i = 0; i < shapes.size(); i++){
               shapes[i]->draw();
               shapes[i]->erase();
               }
            pruge(shapes);
            }
            備注:factory()允許一個(gè)參數(shù)來決定何種類型的Shape對象,這里參數(shù)也可以是string。添加新的Shape類型時(shí),只要修改factory()就可以。
            為確保只能創(chuàng)建在factory()中,Shape把構(gòu)造聲明為私有,Shape有聲明為友元(也可以把Shape::factory()聲明為友元函數(shù)),這樣factory()可以訪問它的構(gòu)造函數(shù)。這樣做的缺點(diǎn):一旦新類型被加入到這種結(jié)構(gòu)來時(shí),必須更新基類。

            posted on 2009-04-12 16:37 Benjamin 閱讀(412) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            97久久精品无码一区二区天美| 久久免费美女视频| 品成人欧美大片久久国产欧美...| 99蜜桃臀久久久欧美精品网站| 国内精品久久久久国产盗摄| 久久综合综合久久97色| 国产精品久久波多野结衣| 久久久久免费看成人影片| 午夜不卡久久精品无码免费| 少妇高潮惨叫久久久久久| 久久精品国产亚洲αv忘忧草| 久久久久久久波多野结衣高潮| 中文字幕无码久久精品青草 | 亚洲国产综合久久天堂| 久久99久久成人免费播放| 久久久久久久亚洲精品 | 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 亚洲人成网亚洲欧洲无码久久| 中文字幕热久久久久久久| 狠狠综合久久综合88亚洲| 欧美大香线蕉线伊人久久| 国产成人久久AV免费| 国产午夜电影久久| 亚洲国产高清精品线久久| 一本久久a久久精品vr综合| 国产午夜久久影院| 亚洲欧美国产日韩综合久久| 午夜精品久久久久久中宇| 久久99精品久久久久婷婷| 国产成人香蕉久久久久| 国产69精品久久久久APP下载| …久久精品99久久香蕉国产| 久久精品国产福利国产琪琪| 欧美亚洲色综久久精品国产| 国内精品久久久久久久久电影网 | 久久电影网一区| 亚洲综合久久夜AV | 狠狠色丁香婷综合久久| 99久久国产亚洲综合精品| 国产精品美女久久久免费| 色偷偷偷久久伊人大杳蕉|