抽象工厂模式就是在工厂模式的基础上,对工厂进行抽象,有很多工厂的实现
特点:产品对象家族
#include <iostream>
#include<string>
using namespace std;
//抽象苹果
class abstract_apple {
public:
virtual void name() = 0;
};
class china_apple :public abstract_apple {
public:
virtual void name() {
<< "apple from china" << endl;
cout }
};
class usa_apple :public abstract_apple {
public:
virtual void name() {
<< "apple from usa" << endl;
cout }
};
class japan_apple :public abstract_apple {
public:
virtual void name() {
<< "apple from japan" << endl;
cout }
};
//抽象香蕉
class abstract_banana {
public:
virtual void name() = 0;
};
class china_banana :public abstract_banana {
public:
virtual void name() {
<< "banana from china" << endl;
cout }
};
class usa_banana:public abstract_banana {
public:
virtual void name() {
<< "banana from usa" << endl;
cout }
};
class japan_banana :public abstract_banana {
public:
virtual void name() {
<< "banana from japan" << endl;
cout }
};
//抽象工厂
class abstract_factory {
public:
virtual abstract_apple* get_apple()=0;
virtual abstract_banana* get_banana()=0;
};
//中国工厂
class china_factory :public abstract_factory {
public:
virtual abstract_apple* get_apple() {
return new china_apple;
}
virtual abstract_banana* get_banana() {
return new china_banana;
}
};
//美国工厂
class usa_factory :public abstract_factory {
public:
virtual abstract_apple* get_apple() {
return new usa_apple;
}
virtual abstract_banana* get_banana() {
return new usa_banana;
}
};
//日本工厂
class japan_factory :public abstract_factory {
public:
virtual abstract_apple* get_apple() {
return new japan_apple;
}
virtual abstract_banana* get_banana() {
return new japan_banana;
}
};
int main(int argc, char** argv)
{
//创建三种工厂
* china = new china_factory;
abstract_factory* usa = new usa_factory;
abstract_factory* japan = new japan_factory;
abstract_factory* apple_china = china->get_apple();
abstract_apple->name();//apple from china
apple_china* banana_usa = usa->get_banana();
abstract_banana->name();//banana from usa
banana_usadelete china;
delete usa;
delete japan;
delete apple_china;
delete banana_usa;
return 0;
}