抽象工厂模式针对的是对产品族,而不是产品等级结构。
产品族: 同一厂商生产的产品。
产品等级: 同一产品,不同厂商的产品。
比如水果类里面由苹果和香蕉,水果就是产品族,苹果香蕉就是产品等级。
// 抽象苹果 产品等级
class AbstractApple
{
public:
virtual void showName() = 0;
};
// 中国苹果
class ChinaApple : public AbstractApple
{
public:
virtual void showName() {
cout << "中国苹果" << endl;
}
};
// 美国苹果
class USAApple : public AbstractApple
{
public:
virtual void showName() {
cout << "美国苹果" << endl;
}
};
// 日本苹果
class JapanApple : public AbstractApple
{
public:
virtual void showName() {
cout << "日本苹果" << endl;
}
};
// 抽象香蕉 产品等级
class AbstractBanana
{
public:
virtual void showName() = 0;
};
// 中国香蕉
class ChinaBanana : public AbstractBanana
{
public:
virtual void showName() {
cout << "中国香蕉" << endl;
}
};
// 美国香蕉
class USABanana : public AbstractBanana
{
public:
virtual void showName() {
cout << "美国香蕉" << endl;
}
};
// 日本香蕉
class JapanBanana : public AbstractBanana
{
public:
virtual void showName() {
cout << "日本香蕉" << endl;
}
};
// 抽象鸭梨 产品等级
class AbstractPear
{
public:
virtual void showName() = 0;
};
// 中国鸭梨
class ChinaPear : public AbstractPear
{
public:
virtual void showName() {
cout << "中国鸭梨" << endl;
}
};
// 美国鸭梨
class USAPear : public AbstractPear
{
public:
virtual void showName() {
cout << "美国鸭梨" << endl;
}
};
// 日本鸭梨
class JapanPear : public AbstractPear
{
public:
virtual void showName() {
cout << "日本鸭梨" << endl;
}
};
// 抽象工厂 针对产品族
class AbstractFactory
{
public:
virtual AbstractApple* creatApple() = 0;
virtual AbstractBanana* creatBanana() = 0;
virtual AbstractPear* creatPear() = 0;
};
// 中国工厂
class ChinaFactory : public AbstractFactory
{
public:
AbstractPear* creatPear(){
return new ChinaPear;
}
AbstractApple* creatApple() {
return new ChinaApple;
}
AbstractBanana* creatBanana() {
return new ChinaBanana;
}
};
// 美国工厂
class USAFactory : public AbstractFactory
{
public:
AbstractPear* creatPear() {
return new USAPear;
}
AbstractApple* creatApple() {
return new USAApple;
}
AbstractBanana* creatBanana() {
return new USABanana;
}
};
// 日本工厂
class JapanFactory : public AbstractFactory
{
public:
AbstractPear* creatPear() {
return new JapanPear;
}
AbstractApple* creatApple() {
return new JapanApple;
}
AbstractBanana* creatBanana() {
return new JapanBanana;
}
};
// 抽象工厂的使用
void test01()
{
AbstractFactory* factory = NULL;
AbstractApple* apple = NULL;
AbstractBanana* banana = NULL;
AbstractPear* pear = NULL;
factory = new ChinaFactory();
apple = factory->creatApple();
banana = factory->creatBanana();
pear = factory->creatPear();
apple->showName();
banana->showName();
pear->showName();
delete factory;
delete apple;
delete banana;
delete pear;
factory = new USAFactory();
apple = factory->creatApple();
banana = factory->creatBanana();
pear = factory->creatPear();
apple->showName();
banana->showName();
pear->showName();
delete factory;
delete apple;
delete banana;
delete pear;
factory = new JapanFactory();
apple = factory->creatApple();
banana = factory->creatBanana();
pear = factory->creatPear();
apple->showName();
banana->showName();
pear->showName();
delete factory;
delete apple;
delete banana;
delete pear;
}