//抽象工厂是针对的产品族,而不是产品等级结构
//产品族:同一产地,或同一厂商 功能不同
//产品等级:功能相同,产地或厂商不同
#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;
}
设计模式-抽象工厂
最新推荐文章于 2024-08-30 08:55:59 发布