简单工厂模式
来源:互联网 发布:p2p h.264网络摄像机 编辑:程序博客网 时间:2024/05/22 14:41
简单工厂模式
1.1UML图
1.2示例代码
class product {
publc:
product() {}
~product() {}
public:
virtual int getbumber() = 0;
}
class productA {
publc:
productA() {}
~productA() {}
public:
int getbumber() { std::cout << " is productB" << std::endl; return 0; }
}
class productB {
publc:
productB() {}
~productB() {}
public:
int getbumber() { std::cout << " is productB" << std::endl; return 1; }
}
class simplefactory {
public:
simplefactory() {}
~simplefactory() {}
public:
product* createproduct(char flag);
}
product* simplefactory:: createproduct(char flag) {
switch(flag) {
case '+' :
return new productA;
break;
case '-':
return new productB;
break;
default:
return NULL;
break;
}
return NULL;
}
int main() {
simplefactory testfactory;
product* baseproduct = testfactory.createproduct('+');
baseproduct.getbumber();
Return 0;
}
1.3分析
1.3.1优缺点分析
缺点: 1.此模式中,业务逻辑的判断都simplefactory 中,当需求发生变化或者 增减的时候,simplefactory 需要变化,违背了开闭原则。
2.simplefactory 包含了所有产品创建的逻辑,随着业务的扩展与修改, 会越来越复杂,风险就增加了。这样的类俗称:”上帝类”
优点: 1.客户端无需关系业务逻辑的细节,只需要向simplefactory 提出需求即 可,即客户端和具体实现类的解耦。
- 工厂模式-简单工厂
- 工厂模式-简单工厂
- 工厂模式-简单工厂模式
- 工厂模式-简单工厂模式
- 简单工厂模式,工厂模式
- 工厂模式-简单工厂模式
- 简单工厂模式,工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂&工厂模式&抽象工厂模式
- 简单工厂和工厂模式
- 简单工厂和工厂模式
- 1,工厂模式--简单工厂
- 简单工厂和工厂模式
- 简单工厂,工厂模式,抽象工厂
- 简单工厂,工厂模式,抽象工厂
- 简单工厂、工厂方法、抽象工厂模式
- 简单工厂、工厂模式和抽象工厂
- Could not find the main class: org.elasticsearch.bootstrap.Elasticsearch. Program will exit.
- DELETE ARCHIVELOG ALL COMPLETED BEFORE/after 'SYSDATE-7'与DELETE ARCHIVELOG UNTIL TIME 'SYSDATE-7'区别
- 测试驱动开发与软件重构
- 百度翻译
- JS中的prototype详解
- 简单工厂模式
- 自己学驱动17——ARM工作模式和ARM9寄存器
- IDEA安装及设置
- Linux下使用crontab自动备份数据库
- httpClient简单例子
- Oozie工作流程定义详解
- 使用 jsoup 对 HTML 文档进行解析和操作
- Java中的字符编码(Unicode、UTF-8、UTF-16)的那些事儿
- Oozie Coordinator使用及详解