• <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ù)傳進來的標(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| 亚洲天堂久久久| 久久亚洲日韩精品一区二区三区| 久久久久国产精品熟女影院| 亚洲精品无码久久久| 伊人久久大香线蕉精品不卡 | 久久精品国产99国产精偷| 国产精品免费福利久久| 久久黄视频| 国产精品久久国产精麻豆99网站| 99久久夜色精品国产网站| 久久精品国产99久久久| yellow中文字幕久久网| 久久精品黄AA片一区二区三区| 精品无码久久久久久久动漫| 国产香蕉久久精品综合网| 狠狠久久综合| 久久狠狠色狠狠色综合| 亚洲女久久久噜噜噜熟女| 久久亚洲中文字幕精品一区四| 国产产无码乱码精品久久鸭| 中文成人无码精品久久久不卡| 9久久9久久精品| 老色鬼久久亚洲AV综合| 精品综合久久久久久97| 久久久黄色大片| 久久99九九国产免费看小说| 狠狠色丁香婷婷综合久久来来去 | 少妇无套内谢久久久久| 久久久久久久久久免免费精品| 久久免费精品一区二区| 国产精品久久99| 久久久综合九色合综国产| 69久久精品无码一区二区| 久久国产色AV免费看| 久久精品亚洲精品国产色婷| 久久人人爽人人爽人人片AV不 | 欧美精品国产综合久久| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 |