设计模式──简单工厂模式
一、 简单工厂模式又称静态工厂方法模式(Static Factory Method),它不是Gof 所讲的23种设计模式之一,但是它却是我们在编码过程中经常使用的方法之一。
1.静态工厂方法统一管理对象的创建。
静态工厂方法通过传入的参数判断决定创建哪一个产品的实例,封装了对象的创建,客户端只管消费,实现了对责任(模块)的分割。
2.静态工厂方法推迟了产品的实例化。
通过XML配置文件就能改变具体要创建的产品实例,修改为其它的产品实例,代码不须重新编译。
二、实现:
class CProduct{ //产品抽象接口
public:
virtual void info()=0;
};
class ProductA::CProduct{
public:
virtual void info(){
cout<<"PoductA."<<endl;
}
};
class ProductB:CProduct{
public:
void info(){
cout<<"ProductB."<<endl;
}
};
class CFactory{
public:
CProduct * produce(char cName){
switch(cName){
case 'A':
return new ProductA();
break;
case 'B':
return new productB();
break;
default:
break;
}
}
};
int main(){
CFactory factory;
CProduct *prodcut=factory.produce('A');
product->info();
return 0;
}