newwy
奋斗在IT路上的小蜗牛。一步一步往上爬,爬到小牛,在到大牛,然后是神牛,然后是犇,然后就可以离开IT行业,回归大自然了。 远离IT,珍爱生命!!! 记录学习的点滴。
/*********************************
*设计模式--抽象工厂实现
*C++语言
*Author:WangYong
*Blog:http://www.cnblogs.com/newwy
********************************/

#include <iostream>
using namespace std;

class AbstractProductA
{
	public:
	virtual ~AbstractProductA() = 0;
	AbstractProductA(){};
};
AbstractProductA::~AbstractProductA(){}

class ProductA1:public AbstractProductA
{
	public:
		ProductA1(){cout<<"ProductA1"<<endl;}	
};

class ProductA2:public AbstractProductA
{
	public:
		ProductA2(){cout<<"ProductA2"<<endl;}	
};

class AbstractProductB
{
	public:
	virtual ~AbstractProductB() = 0;
	AbstractProductB(){};
};
AbstractProductB::~AbstractProductB(){}

class ProductB1:public AbstractProductB
{
	public:
		ProductB1(){cout<<"ProductB1"<<endl;}	
};

class ProductB2:public AbstractProductB
{
	public:
		ProductB2(){cout<<"ProductB2"<<endl;}	
};

class AbstractFactory
{
	public:
	virtual ~AbstractFactory();
	virtual AbstractProductA *CreateProductA() = 0;
	virtual AbstractProductB *CreateProductB() = 0;
};
AbstractFactory::~AbstractFactory(){}
class ConcreteFactory1:public AbstractFactory
{
	public:
	ConcreteFactory1(){cout<<"ConcreteFactory1..."<<endl;}
	AbstractProductA *CreateProductA()  {return new ProductA1();}
	AbstractProductB *CreateProductB()  {return new ProductB1();}
};

class ConcreteFactory2:public AbstractFactory
{
	public:
	ConcreteFactory2(){cout<<"ConcreteFactory2..."<<endl;}
	AbstractProductA *CreateProductA(){return new ProductA2();}
	AbstractProductB *CreateProductB(){return new ProductB2();}
};

int main()
{
	AbstractFactory *cf1 = new ConcreteFactory1();
	cf1->CreateProductA();cf1->CreateProductB();
	
	AbstractFactory *cf2 = new ConcreteFactory2();
	cf2->CreateProductA();cf2->CreateProductB();
	return 0;
}

posted on 2010-10-18 23:27  newwy  阅读(700)  评论(0编辑  收藏  举报