• <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 閱讀(415) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            狠狠色丁香久久婷婷综合蜜芽五月 | 久久久久久亚洲Av无码精品专口| 久久av免费天堂小草播放| 欧美久久久久久午夜精品| 伊人久久五月天| 久久中文字幕一区二区| 久久亚洲精品国产精品婷婷| 国内精品久久久久| 中文无码久久精品| 色综合合久久天天给综看| 色综合久久无码五十路人妻| 久久综合色之久久综合| 久久精品国产91久久麻豆自制 | 久久久久国产日韩精品网站 | 久久亚洲色一区二区三区| 精品久久久久久中文字幕人妻最新 | 日本福利片国产午夜久久| 久久久久亚洲av无码专区喷水| 伊人精品久久久久7777| 日本久久中文字幕| 久久精品国产精品亚洲艾草网美妙 | 久久久亚洲裙底偷窥综合| 中文字幕无码免费久久| 久久人人爽人人爽人人片AV东京热 | 久久久精品日本一区二区三区| 久久青草国产精品一区| 久久精品成人一区二区三区| 久久久久久久亚洲精品| 亚洲va国产va天堂va久久| 久久香综合精品久久伊人| 999久久久无码国产精品| 国产成人综合久久综合| 国产午夜精品理论片久久| 一级a性色生活片久久无少妇一级婬片免费放 | 久久r热这里有精品视频| 中文字幕无码久久人妻| 欧美噜噜久久久XXX| 精产国品久久一二三产区区别| 久久九九精品99国产精品| 国产精品乱码久久久久久软件| 久久久国产精品福利免费 |