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

            設(shè)計模式之factory工廠模式(二):polymorphic factory多態(tài)工廠

            factory工廠模式實際上polymorphic factory多態(tài)工廠的特例。
            #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;//不能直接調(diào)用,可以覆蓋
               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())//找工廠對象根據(jù)傳進(jìn)來的標(biāo)識符
                  return factories[id]->create();
              else
                  throw BadShapeCreation(id);
               }
            };//Shape對象的創(chuàng)建處

            //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);
               }

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

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

            久久天天躁狠狠躁夜夜av浪潮 | 草草久久久无码国产专区| 少妇内射兰兰久久| 亚洲一级Av无码毛片久久精品| 久久久无码精品亚洲日韩蜜臀浪潮| 国产激情久久久久影院老熟女免费 | 国产三级精品久久| 丁香久久婷婷国产午夜视频| 久久久久无码精品国产app| 精品国产乱码久久久久软件| 91精品国产综合久久婷婷 | 香蕉久久夜色精品国产小说| 久久亚洲国产精品123区| 久久精品人人做人人爽电影蜜月 | 久久A级毛片免费观看| 久久996热精品xxxx| 亚洲综合日韩久久成人AV| 四虎国产永久免费久久| 欧洲成人午夜精品无码区久久| 欧美精品一区二区精品久久| 色综合久久88色综合天天 | 久久中文字幕一区二区| 婷婷久久久亚洲欧洲日产国码AV | 日韩十八禁一区二区久久| 国产精品久久久久久| 一本色道久久HEZYO无码| 久久99精品久久久久久不卡| 97久久精品无码一区二区天美| 亚洲а∨天堂久久精品9966| 国产成人久久精品激情| 无码任你躁久久久久久老妇App| 青青草原1769久久免费播放| 亚洲精品无码成人片久久| 少妇人妻综合久久中文字幕| 四虎国产精品免费久久久| 国产精品久久久久久搜索| 亚洲人成伊人成综合网久久久| 99久久国产综合精品五月天喷水| 精品免费久久久久久久| 久久精品国产亚洲αv忘忧草 | 国产L精品国产亚洲区久久|