当前位置:网站首页>简单工厂模式
简单工厂模式
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;
}
边栏推荐
- Constructor, this keyword, method overloading, local variables and member variables
- go笔记记录——channel
- 管理基础知识20
- nodeJs--mime module
- 【目标检测】FCOS: Fully Convolutional One-Stage Object Detection
- nodeJs--mime模块
- Transient Stability Distributed Control of Power System with External Energy Storage
- c语言字符和字符串函数总结(二)
- bgp 聚合 反射器 联邦实验
- unity2D横版游戏教程5-UI
猜你喜欢
随机推荐
go笔记记录——channel
146. LRU cache
Trie详解
路由策略
Kotlin协程:创建、启动、挂起、恢复
哪里有期货开户的正规途径?
460. LFU cache
好的期货公司开户让人省心省钱
These 4 computer notepad software, you have to try
JSP how to obtain the path information in the request object?
Simpson's paradox
管理基础知识13
js中内存泄漏的几种情况
460. LFU 缓存
期货开户调整交易所保证金标准
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
管理基础知识11
抖音数据接口API-获取用户主页信息-监控直播开启
Industrial control network intrusion detection based on automatic optimization of hyperparameters
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?