当前位置:网站首页>简单工厂模式
简单工厂模式
2022-08-02 00:16:00 【l_ethan】
定义:定义了一个创建对象的类,由这个类来封装实例化对象的行为。
优点:
1.客户端和具体实现类解耦
2.对于某些对象创建过程比较复杂,我们不考虑
缺点:
1.简单工厂模式,增加新的功能是通过修改源代码实现,不符合开闭原则
2.这个类职责过重,这个类发生问题会影响很多使用这个工厂的实例
案例
#include <iostream>
using namespace std;
class AbstractFruit {
public:
virtual void show() = 0;
};
class Apple :public AbstractFruit {
public:
virtual void show() {
cout << "apple" << endl;
}
};
class Banana :public AbstractFruit {
public:
virtual void show() {
cout << "banana" << endl;
}
};
class FruitFactor {
public:
static AbstractFruit* createFruit(string flag) {
if (flag == "apple")
return new Apple;
else if (flag == "banana")
return new Banana;
else {
return NULL;
}
}
};
int main()
{
FruitFactor* factory = new FruitFactor;
AbstractFruit* fruit = factory->createFruit("apple");
fruit->show();
return 0;
}
边栏推荐
- Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
- JS中对作用域链的理解(查找变量)
- Redis 相关问题
- 思维导图,UML在线画图工具
- JS中清空数组的方法
- DOA从一维阵列传感说起
- 请教一下本网站左下角的动漫人物是怎么做的?
- 【HCIP】BGP小型实验(联邦,优化)
- How does JSP use the page command to make the JSP file support Chinese encoding?
- 期货开户如何确定期货公司正规性?
猜你喜欢
随机推荐
工业信息物理系统攻击检测增强模型
哪里有期货开户的正规途径?
Grid false data injection attacks detection based on coding strategy
ES6对箭头函数的理解
JS中对事件代理的理解及其应用场景
Redis的集群模式
交返是做日内交易的必要条件
不要用jOOQ串联字符串
Looking back at 5 recessionary times in history: How might this time be different?
JSP 如何获取request对象中的路径信息呢?
nodeJs--mime module
Redis 相关问题
21.数据增强
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
辨析内存函数memset、memcmp、memmove以及memcpy
Kunpeng compile and debug plug-in actual combat
如何期货开户和选择期货公司?
什么是低代码(Low-Code)?低代码适用于哪些场景?
Are test points the same as test cases?
What is the function of the JSP Taglib directive?









