• <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)模式(一)

            factory工廠模式是所有模式中最有用的模式之一。它強制用一個factory工廠來創建對象,程序中所有需創建對象的代碼都在這個factory中執行,它是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()允許一個參數來決定何種類型的Shape對象,這里參數也可以是string。添加新的Shape類型時,只要修改factory()就可以。
            為確保只能創建在factory()中,Shape把構造聲明為私有,Shape有聲明為友元(也可以把Shape::factory()聲明為友元函數),這樣factory()可以訪問它的構造函數。這樣做的缺點:一旦新類型被加入到這種結構來時,必須更新基類。

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

            亚洲乱亚洲乱淫久久| 久久精品亚洲精品国产色婷| 欧洲国产伦久久久久久久| 久久伊人亚洲AV无码网站| 亚洲国产日韩欧美久久| 久久成人国产精品免费软件| 国产精品一区二区久久| 久久国产乱子伦精品免费午夜| 欧美午夜精品久久久久久浪潮| 久久人人爽人人爽人人av东京热| 久久精品国产第一区二区三区 | 久久天天躁狠狠躁夜夜2020老熟妇| 欧美激情精品久久久久久久九九九| 中文字幕人妻色偷偷久久 | 国产精品一久久香蕉产线看 | 狠狠精品久久久无码中文字幕| 国产精品久久久久久| 亚洲精品无码久久久| 久久国产色AV免费观看| 欧美久久一区二区三区| 九九久久99综合一区二区| 欧美久久综合九色综合| 久久91综合国产91久久精品| 久久99国产精品久久99小说| 亚洲国产成人久久综合碰碰动漫3d| 国产免费久久精品99re丫y| 久久福利青草精品资源站| 狠狠精品久久久无码中文字幕| 精品久久国产一区二区三区香蕉| 亚洲午夜久久久影院| 中文字幕精品久久久久人妻| 久久精品国产精品青草| 久久久久国产精品嫩草影院| 国产成人精品久久一区二区三区av| 亚洲日韩中文无码久久| 亚洲国产成人久久综合一区77| 久久99精品国产一区二区三区| 亚洲色欲久久久综合网东京热| 亚洲а∨天堂久久精品| 国产毛片久久久久久国产毛片| 国内精品久久久久久久97牛牛|