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

            久久精品成人免费国产片小草| 国产精品久久99| 韩国三级中文字幕hd久久精品 | 久久精品国产AV一区二区三区| 亚洲精品综合久久| 久久天天躁狠狠躁夜夜不卡| 久久久久久九九99精品| 韩国三级中文字幕hd久久精品| 中文字幕久久亚洲一区| 久久狠狠高潮亚洲精品 | 久久激情亚洲精品无码?V| 老司机午夜网站国内精品久久久久久久久 | 久久偷看各类wc女厕嘘嘘| 99热都是精品久久久久久| 久久久久免费精品国产| 91亚洲国产成人久久精品网址| 国产美女亚洲精品久久久综合| 亚洲一本综合久久| 奇米影视7777久久精品| 日批日出水久久亚洲精品tv| 9久久9久久精品| 久久精品国产亚洲精品2020| 久久亚洲色一区二区三区| 亚洲综合婷婷久久| 国产精品久久网| 人妻无码αv中文字幕久久琪琪布| 色综合久久久久综合99| 亚洲国产精品久久久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 欧美精品一区二区久久| 中文字幕久久欲求不满| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 狠狠色婷婷综合天天久久丁香| 亚洲中文久久精品无码| 亚洲日本va午夜中文字幕久久 | 亚洲AV无一区二区三区久久 | 亚洲αv久久久噜噜噜噜噜| 久久成人小视频| 久久综合九色综合网站| 亚洲中文字幕无码久久2020| 亚洲综合日韩久久成人AV|