c++实现简单工厂、工厂方法、抽象工厂模式:
题目描述:
(1)、简单工厂模式
使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。
#include<iostream>
using namespace std;
class Person {
public :
virtual void showSelf()=0;
};
class Man : public Person{
public :
void showSelf(){
cout<<"I am a man."<<endl;
}
};
class WoMan : public Person{
public :
void showSelf(){
cout<<"I am a woman."<<endl;
}
};
class Robot : public Person{
public :
void showSelf(){
cout<<"I am a Robot."<<endl;
}
};
class NvWa{
public :
static Person * createPerson(char type){
Person * p;
switch (type){
case 'W':
p=new WoMan();
break;
case 'M':
p=new Man();
break;
case 'R':
p=new Robot();
break;
// 需要添加新的产品品种时,需要添加新的case分行:违反关闭修改
}
return p;
}
};
// 进行测试
int main(void){
Person * per=NvWa::createPerson('W');
per->showSelf();
delete per;
return 0;
}
(2)、工厂方法模式
海尔工厂(Haier)生产海尔空调(HaierAirCondition),美的工厂(Midea)生产美的空调(MideaAirCondition) 。使用工厂方法模式描述该场景,绘制类图并编程实现。
#include<iostream>
using namespace std;
// 抽象产品类
class AirCondition{
public :
virtual void show()=0;
};
class MideaAirCondition : public AirCondition{
public :
void show(){
cout<<"美的空调"<<endl;
}
};
class HaierAirCondition : public AirCondition{
public :
void show(){
cout<<"海尔空调"<<endl;
}
};
/*
每个产品的工厂方法类:负责生产特定的产品
*/
class MideaFactoryMethod{
public :
static AirCondition * createAirCondition(){
return new MideaAirCondition();
}
};
class HaierFactoryMethod {
public :
static AirCondition * createAirCondition(){
return new HaierAirCondition();
}
};
// 测试
int main(void){
AirCondition * haier= HaierFactoryMethod::createAirCondition();
haier->show();
AirCondition * midea= MideaFactoryMethod::createAirCondition();
midea->show();
delete haier;
delete midea;
return 0;
}
(3)、抽象工程模式
电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU的品牌、型号并不一定相同,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制相应的类图,绘制类图并编程实现。
#include<iostream>
using namespace std;
// 抽象空调类
class AirCondition{
public :
virtual void showCondition()=0;
};
class MideaAirCondition : public AirCondition{
public :
void showCondition(){
cout<<"美的空调"<<endl;
}
};
class HaierAirCondition : public AirCondition{
public :
void showCondition(){
cout<<"海尔空调"<<endl;
}
};
// 抽象电视机类
class TV{
public :
virtual void showTV()=0;
};
class HaierTV : public TV{
public :
void showTV(){
cout<<"海尔电视机"<<endl;
}
};
class MideaTV : public TV{
public :
void showTV(){
cout<<"美的电视机"<<endl;
}
};
// 抽象工厂
class MideaFactory{
public :
static AirCondition * createAirCondition(){
return new MideaAirCondition();
}
static TV * createTV(){
return new MideaTV();
}
};
class HaierFactory{
public :
static AirCondition * createAirCondition(){
return new HaierAirCondition();
}
static TV * createTV(){
return new HaierTV();
}
};
// 测试
int main(void){
AirCondition * haierCon= HaierFactory::createAirCondition();
haierCon->showCondition();
TV * haierTV=HaierFactory::createTV();
haierTV->showTV();
cout<<"--------------------"<<endl;
AirCondition * MideaCon= MideaFactory::createAirCondition();
MideaCon->showCondition();
TV * MideaTV=MideaFactory::createTV();
MideaTV->showTV();
delete haierCon;
delete haierTV;
delete MideaCon;
delete MideaTV;
return 0;
}