现在的位置: 首页 > 综合 > 正文

GoF的Abstract factory的c/c++实现

2018年10月18日 ⁄ 综合 ⁄ 共 3551字 ⁄ 字号 评论关闭

                                                                Abstract   factory

                       作者:周顺利

     这个是我理解GoF的abstract factory,最近没事自己按自己理解写的.bcb 6.0下编译正确.

//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#include <iostream>

 

using namespace std;

class Product
{
public:
        virtual void Creat()=0 ;
};
class myProductA:public Product
{
public:
        myProductA()
        {
                this->Creat();
        }
        void  Creat()
        {
                cout<<"my ProductA is Created!"<<endl;

        }
};
class myProductB:public Product
{
public:
        myProductB()
        {
                this->Creat();
        }
        void  Creat()
        {
                cout<<"myProductB is created!"<<endl;

        }
};

class uProductA:public Product
{
public:
        uProductA()
        {
                this->Creat();
        }
        void Creat()
        {
                cout<<"your ProductA is created!"<<endl;

        }
};
class uProductB:public Product
{
public:
        uProductB()
        {
                this->Creat();
        }
        void Creat()
        {
                cout<<"your productB is created!"<<endl;

        }
};

class Factory
{
public:
        virtual Product* CreateProductA()=0;
        virtual Product* CreateProductB()=0;

};
class myFactory:public Factory
{
public:
        Product* CreateProductA()
        {
                cout<<"my ProductA is returned!"<<endl;
                return new myProductA;
        }
        Product* CreateProductB()
        {
                cout<<" my ProductB is returned!"<<endl;
                 return new myProductB;
        }
};
class uFactory:public Factory
{
public:
        Product* CreateProductA()
        {
                cout<<"your productA is returned!"<<endl;
                return new uProductA;
        }
        Product* CreateProductB()
        {
                cout<<"your productB is returned!"<<endl;
                return new uProductB;

        }
private:

};

class Object
{
public:
        void AddProductA(Product& proA)
        {
                cout<<"A ProductA is add to the gui!"<<endl;
        }
        void  AddProductB(Product& proB)
        {
                cout<<"A ProductB is add to gui!"<<endl;
        }

};

 

int main(int argc, char* argv[])
{
        Object* obj=new Object();
        Factory* factory;
        cout<<"Please input the type of the obj :u/m"<<endl;
        char c;
        cin>>c;
        if(c=='m')
        {
                factory=new myFactory;
        }
        else if(c=='u')
        {
                factory=new uFactory;
        }
        Product* proA;
        Product* proB;
        proA=factory->CreateProductA();
        proB=factory->CreateProductB();
        obj->AddProductA(*proA);
        obj->AddProductB(*proB);

        char f;
        if(cin>>f)
  

        return 0;
}
//---------------------------------------------------------------------------

下边是一个factory method ,是我前几天在bbs上看见的,也发上来,以便factory 模式更完整.

#pragma warning(disable : 4786)

#include <iostream>

using namespace std;

class Base
{
public:
    virtual void displayName()
    {  cout << "Base" << endl;}
    virtual ~Base(){}
};

class  Drive1: virtual public Base
{
public:
    virtual void displayName()
    {  cout << "Drive1" << endl;}
};

class  Drive2: virtual public Base
{
public:
    virtual void displayName()
    {  cout << "Drive2" << endl;}
};

class Factory
{
public:
    static Base *Creater(const char *name)
    {
        if (strcmp(name, "Drive1") == 0)
        {
            return new Drive1;
        }
        else if (strcmp(name, "Drive2") == 0)
        {
            return new Drive2;
        }
        else
        {   return NULL; }
    }
};

void main()
{
    Base *d1 = Factory::Creater("Drive1");
    Base *d2 = Factory::Creater("Drive2");
    d1->displayName();
    d2->displayName();

    delete d1;
    delete d2;
}

【上篇】
【下篇】

抱歉!评论已关闭.