抽象工厂模式-- 5种创建型模式之一

 

# include <iostream>

using namespace std;

//抽象产品A
class AbsProductA {
public:
	virtual void show() = 0;
};

//抽象产品B
class AbsProductB {
public:
	virtual void show() = 0;
};

//抽象工厂
class AbsFactory {
public:
	virtual AbsProductA *newProductA() = 0;
	virtual AbsProductB *newProductB() = 0;
};

//具体产品A_1
class ProductA_1 : public AbsProductA {
public:
	virtual void show() {
		cout << "具体产品A_1" << endl;
	}
};

//具体产品A_2
class ProductA_2 : public AbsProductA {
public:
	virtual void show() {
		cout << "具体产品A_2" << endl;
	}
};

//具体产品B_1
class ProductB_1 : public AbsProductB {
public:
	virtual void show() {
		cout << "具体产品B_1" << endl;
	}
};

//具体产品B_2
class ProductB_2 : public AbsProductB {
public:
	virtual void show() {
		cout << "具体产品B_2" << endl;
	}
};

//具体工厂1
class Factory1 : public AbsFactory {
public:
	virtual AbsProductA *newProductA() {
		return new ProductA_1;
	}
	virtual AbsProductB *newProductB() {
		return new ProductB_1;
	}
};

//具体工厂2
class Factory2 : public AbsFactory {
public:
	virtual AbsProductA *newProductA() {
		return new ProductA_2;
	}
	virtual AbsProductB *newProductB() {
		return new ProductB_2;
	}
};

int main()
{
	//具体工厂1
	Factory1 fatory1;
	AbsProductA *productA1 = fatory1.newProductA();
	productA1->show();
}

  

posted @ 2017-09-06 17:21  宁静淡泊  阅读(95)  评论(0编辑  收藏  举报