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

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 398, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            設計模式之factory工廠模式(二):polymorphic factory多態工廠

            factory工廠模式實際上polymorphic factory多態工廠的特例。
            #include <iostream>
            #include <map>
            #include <string>
            #include <vector>
            #include <stdexcept>
            #include <cstddef>
            #include "../purge.h"
            using namespace std;

            class Shape{
            public:
               virtual void draw()=0;
               virtual void erase()=0;
               virtual ~Shape();
            };

            class ShapeFactory{
               virtual Shape* create()=0;//不能直接調用,可以覆蓋
               static map<string,ShapeFactory*> factories;
            public:
               virtual ~ShapeFactory() {}
               friend BadShapeFactoryInitializer;//map和工廠對象的初始化
               class BasdShapeCreation:public logic_error{
            public:
               BagShapeCreation(string type):logic_error("Cannot create type"+type){}
            };
            static Shape* creatShape(const stirng& id) throw(BadShapeCreation){
               if(factories.find(id) != factories.end())//找工廠對象根據傳進來的標識符
                  return factories[id]->create();
              else
                  throw BadShapeCreation(id);
               }
            };//Shape對象的創建處

            //define the static object
            map<string,ShapeFactory*> ShapeFactory::factories;

            class Circle:public Shape{
               Circle();//private constructor
               friend class ShapeFactoryInitializer;
               class Factory;
               friend class Factory;
               class Factory:public ShapeFactory{
            public:
               Shape* create(){return new Circle;}
               friend class ShapeFactoryIninitializer;
            };
            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 ShapeFactoryInitializer;
               class Factory;
               friend class Factory;
               class Factory:public ShapeFactory{
            public:
               Shape* create(){return new Square;}
               friend class ShapeFactoryIninitializer;
            };
            public:
               void draw(){cout << "Square::draw"<<endl;}
               void erase(){cout << "Square::erase"<<endl;}
               ~Square(){cout<<"Square::~Square"<<endl;}
            };

            //Singleton to initialize the ShapeFactory:
            class ShapeFactoryInitializer{
               static ShapeFactoryInitializer si;
               ShapeFactoryInitializer(){
               ShapeFactory::factories["Circle"] = new Circle::Factory;
               ShapeFactory::factiories["Square"] = new Square::Factory;
               }
               ~ShpaeFactoryInitializer(){
               map<string,ShapeFactory*>::iterator it= ShapeFactory::factories.begin();
               while(it != ShapeFactory::factories.end())
               delete  it++->second;
               }
            };

            //static member definition
            ShapeFactoryInitializer ShapeFactoryInitializer::si;

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

            int main()
            {
            vector<Shpae*> shapes;
            try{
               for(size_t i=0; i <sizeof(si)/sizeof(si[0]); i++)
               {
                     shape.push_back(ShapeFactory::createShape(si[i]));
               }catch(ShapeFactory::BadShapeCreation e){
                  cout<<e.what()<<endl;
                  return EXIT_FAILURE;
               }
               for(size_t i = 0; i <shape.size();i++)
               {
                  shapes[i]->draw();
                  shapes[i]->earse();
               }
                  purge(shapes);
               }

            這里如果不需要創建單獨的工廠對象,盡可能使用static工廠模式。

            posted on 2009-04-13 23:05 Benjamin 閱讀(423) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            成人资源影音先锋久久资源网| 亚洲日韩中文无码久久| 99久久精品免费观看国产| 久久久综合香蕉尹人综合网| 看全色黄大色大片免费久久久| 久久综合给合久久狠狠狠97色69 | 久久国产精品77777| 久久狠狠一本精品综合网| 午夜天堂av天堂久久久| 大蕉久久伊人中文字幕| 精品国产青草久久久久福利| 色综合色天天久久婷婷基地| 狠狠色狠狠色综合久久| 久久99久久成人免费播放| 久久精品黄AA片一区二区三区| 久久久久人妻精品一区三寸蜜桃| 99久久人妻无码精品系列蜜桃| 日本加勒比久久精品| 精品人妻伦一二三区久久| 97久久精品国产精品青草| 无码精品久久久久久人妻中字| 日日狠狠久久偷偷色综合96蜜桃| 久久久久中文字幕| 91精品国产综合久久婷婷| 国产成人精品综合久久久久| 久久只这里是精品66| 人人狠狠综合久久亚洲| 久久99精品免费一区二区| 香港aa三级久久三级| 99久久国产免费福利| 伊人色综合久久天天| 欧美久久精品一级c片片| 久久国产精品国产自线拍免费| 久久久久无码精品国产| 人妻少妇久久中文字幕 | 久久99精品久久久大学生| 亚洲精品第一综合99久久| 久久久午夜精品福利内容| 思思久久好好热精品国产| 精品久久久久成人码免费动漫 | 久久91亚洲人成电影网站|