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

            久久久久久狠狠丁香| 亚洲午夜久久久久久久久久| 国产精品久久午夜夜伦鲁鲁| 久久99精品国产麻豆| 97久久超碰成人精品网站| 久久美女网站免费| 亚洲乱码日产精品a级毛片久久 | 久久久久久综合一区中文字幕 | 国产真实乱对白精彩久久| 久久久综合香蕉尹人综合网| 亚洲午夜无码久久久久| 久久精品成人免费国产片小草| 久久久亚洲欧洲日产国码是AV| 久久99精品国产99久久| 无码任你躁久久久久久| 国内精品久久久久影院日本| 久久久久国产成人精品亚洲午夜| 日本人妻丰满熟妇久久久久久| 精品欧美一区二区三区久久久| 日韩精品久久无码人妻中文字幕 | 亚洲色欲久久久综合网东京热| 国产精品美女久久久久av爽 | 亚洲国产精品狼友中文久久久| 欧美精品久久久久久久自慰| 久久久久久久久久久免费精品| 久久99精品久久久久久hb无码| 久久伊人五月丁香狠狠色| 精品国产热久久久福利| 久久这里只有精品首页| 91精品国产高清久久久久久io | 香蕉久久AⅤ一区二区三区| 91精品观看91久久久久久| 久久精品国产亚洲av高清漫画| 久久国语露脸国产精品电影| 免费一级做a爰片久久毛片潮| 伊人久久免费视频| 品成人欧美大片久久国产欧美...| 丁香狠狠色婷婷久久综合| 久久久久久久97| 97超级碰碰碰碰久久久久| 99久久精品免费国产大片|