设计模式-抽象工厂

//抽象工厂是针对的产品族,而不是产品等级结构
//产品族:同一产地,或同一厂商  功能不同
//产品等级:功能相同,产地或厂商不同

#include <iostream>
using namespace std;

//抽象苹果
class AbstractApple {
public:
	virtual void ShowName() = 0;
};

//中国苹果
class ChinaApple :public AbstractApple
{
public:
	virtual void ShowName() {
		cout << "中国苹果" << endl;
	}

};

//中国苹果
class JapanApple :public AbstractApple
{
public:
	virtual void ShowName() {
		cout << "日本苹果" << endl;
	}

};
class USAApple :public AbstractApple
{
public:
	virtual void ShowName() {
		cout << "美国苹果" << endl;
	}

};

//抽象香蕉
class AbsrtactBanana {
public :
	virtual void ShowName() = 0;
};

class ChinaBanana :public AbsrtactBanana {
public:
	virtual void ShowName()
	{
		cout << "中国香蕉" << endl;
	}
};

class USABanana :public AbsrtactBanana {
public:
	virtual void ShowName()
	{
		cout << "美国香蕉" << endl;
	}
};

class JapanBanana :public AbsrtactBanana {
public:
	virtual void ShowName()
	{
		cout << "日本香蕉" << endl;
	}
};

//抽象工厂 针对产品族
class AbstracFactory {
public:
	virtual AbstractApple* CreateApple() = 0;
	virtual AbsrtactBanana* CreateBanana() = 0;

};


class ChinaFactory :public AbstracFactory {
public:
	virtual AbstractApple* CreateApple()
	{
		return new ChinaApple;
	}
	virtual AbsrtactBanana* CreateBanana() {
		return new ChinaBanana;
	}
};

class USAFactory :public AbstracFactory {
public:
	virtual AbstractApple* CreateApple()
	{
		return new USAApple;
	}
	virtual AbsrtactBanana* CreateBanana() {
		return new USABanana;
	}
};

class JapanFactory :public AbstracFactory {
public:
	virtual AbstractApple* CreateApple()
	{
		return new JapanApple;
	}
	virtual AbsrtactBanana* CreateBanana() {
		return new JapanBanana;
	}
};


void test01()
{
	AbstracFactory* factory = NULL;
	AbstractApple * apple = NULL;
	AbsrtactBanana* banana = NULL;

	factory = new ChinaFactory;
	apple = factory->CreateApple();
	banana = factory->CreateBanana();

	apple->ShowName();
	banana->ShowName();
	delete banana;
	delete apple;
	delete factory;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值