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

            久久青青国产| 亚洲国产精品一区二区久久hs| 狠狠色综合网站久久久久久久高清| 久久青青草视频| 久久婷婷国产综合精品| 伊人久久大香线焦综合四虎| 一97日本道伊人久久综合影院| 久久人人爽人人爽人人片av高请| 99精品久久久久中文字幕| 久久精品无码一区二区app| 久久精品国产亚洲AV蜜臀色欲| 久久精品人人槡人妻人人玩AV | 青青青青久久精品国产h久久精品五福影院1421 | 久久一区二区三区免费| 久久综合给合久久狠狠狠97色 | 2021国产精品久久精品| 天天久久狠狠色综合| 亚洲精品白浆高清久久久久久| 成人亚洲欧美久久久久| 色欲综合久久中文字幕网| 久久精品18| 国产精品久久波多野结衣| 亚洲日本久久久午夜精品| 久久99精品久久久久久齐齐| 久久国产一区二区| 大伊人青草狠狠久久| 久久精品国产亚洲77777| 国产69精品久久久久APP下载| 精品无码人妻久久久久久| 97久久超碰国产精品旧版| 久久狠狠爱亚洲综合影院| 国内精品伊人久久久影院| 久久免费视频一区| 久久精品国产精品亚洲人人| 亚洲国产成人久久精品影视| 久久久国产精品网站| 久久国产乱子伦精品免费强| WWW婷婷AV久久久影片| 99久久99这里只有免费的精品| 久久亚洲中文字幕精品有坂深雪| 日本久久久久亚洲中字幕|