当前位置:网站首页>简单工厂模式
简单工厂模式
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;
}
边栏推荐
- 期货开户是否有资金门槛?
- How does JSP use the page command to make the JSP file support Chinese encoding?
- 使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
- uni-app project summary
- 期货开户交返是行内公开的秘密
- 06-SDRAM : SDRAM control module
- 管理基础知识12
- Multi-feature fusion face detection based on attention mechanism
- 【HCIP】BGP小型实验(联邦,优化)
- Angr(十二)——官方文档(Part3)
猜你喜欢
随机推荐
管理基础知识11
管理基础知识21
Don't concatenate strings with jOOQ
DFS详解
管理基础知识15
bgp aggregation reflector federation experiment
2022/08/01 学习笔记 (day21) 泛型和枚举
辨析内存函数memset、memcmp、memmove以及memcpy
2022/08/01 Study Notes (day21) Generics and Enums
管理基础知识14
【目标检测】FCOS: Fully Convolutional One-Stage Object Detection
How does JSP use request to get the real IP of the current visitor?
管理基础知识19
Quick solution for infix to suffix and prefix expressions
期货开户是否有资金门槛?
String splitting function strtok exercise
好的期货公司开户让人省心省钱
The Statement update Statement execution
How to design a circular queue?Come and learn~
含外部储能的电力系统暂态稳定分布式控制









