分类: C/C++
2009-01-29 10:36:51
This is the First Arcticle: ABSTRACT FACTORY
Here the theory goes:
/********************************************************************
created:
filename:
file path:
file base:
file ext:
author: 
purpose:
*********************************************************************/

#include <iostream>
using namespace std;
//the abstract implementation
class AbsProductImp
{
public:
virtual void DrawProductShape()=0;
};
//the abstract factory
class AbsFactory
{
public:
virtual AbsProductImp * CreateAProductImp()=0; //Create a real implement
};
//the abstract product
class AbsProduct
{
public:
//virtual void DrawProductShape();
protected:
AbsProductImp * _imp; //implement pointer , it's decide which implement be execute
AbsFactory * _Fac; // this pointer will point to the real factory
};
class AProductImp :public AbsProductImp //the real a product
{
public:
void DrawProductShape()
{
cout<<"A product is describing itself; A is implement complete "<<endl;
}
};
class BProductImp :public AbsProductImp //the real a product
{
public:
virtual void DrawProductShape()
{
cout<<"B product is describing itself; B is implement complete "<<endl;
}
};
class AFactory :public AbsFactory //The factory that create A
{
public:
virtual AbsProductImp * CreateAProductImp()
{
return new AProductImp;
}
};
class BFactory : public AbsFactory
{
public:
virtual AbsProductImp * CreateAProductImp()
{
return new BProductImp;
}
};
class AProduct :public AbsProduct
{
public:
AProduct()
{
_Fac=new AFactory; // specific the AFactory to create A
_imp=_Fac->CreateAProductImp(); // polymorphism , select the product implement
_imp->DrawProductShape(); //construct it self
}
};
class BProduct :public AbsProduct
{
public:
BProduct()
{
_Fac=new BFactory; // specific the BFactory to create B
_imp=_Fac->CreateAProductImp(); // polymorphism , select the product implement
_imp->DrawProductShape(); //construct it self
}
};
int main(int argc, char* argv[])
{
AProduct _a; //Create An AProduct
BProduct _b; //Create A BProduct
return 1;
}


