• <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)德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            設計模式之工廠(factory)模式(一)

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

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

            欧美精品福利视频一区二区三区久久久精品 | 久久久精品午夜免费不卡| 伊人色综合九久久天天蜜桃| 亚州日韩精品专区久久久| 久久无码专区国产精品发布| 国产精品九九久久免费视频 | 欧美日韩精品久久久久| 久久亚洲中文字幕精品有坂深雪| 99久久99久久| 久久婷婷色综合一区二区| 久久婷婷久久一区二区三区| 性高湖久久久久久久久AAAAA| 久久婷婷国产综合精品| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久婷婷五月综合97色| 99热成人精品免费久久| 亚洲va久久久噜噜噜久久狠狠| 精品久久久久香蕉网| 无码任你躁久久久久久老妇| 亚洲综合精品香蕉久久网97| 色综合久久中文字幕无码| 亚洲国产成人精品久久久国产成人一区二区三区综 | 一级做a爰片久久毛片毛片| 人妻精品久久无码专区精东影业| 久久99国产精品久久99果冻传媒 | 久久99精品国产麻豆蜜芽| 久久99精品久久久久久久不卡| 怡红院日本一道日本久久| 久久久久亚洲AV无码网站| 奇米影视7777久久精品人人爽| 久久国产综合精品五月天| 伊人久久大香线蕉影院95| 99久久无色码中文字幕 | 久久91这里精品国产2020| 国产精品久久久久久久久| 久久99热只有频精品8| 少妇人妻88久久中文字幕| 久久成人国产精品免费软件| 久久婷婷国产剧情内射白浆| 日日噜噜夜夜狠狠久久丁香五月| 久久久久人妻一区二区三区|