当前位置:网站首页>简单工厂模式
简单工厂模式
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;
}
边栏推荐
- Statement执行update语句
- Kunpeng compile and debug plug-in actual combat
- 基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
- nodeJs--mime模块
- JSP out.write()方法具有什么功能呢?
- 什么是低代码(Low-Code)?低代码适用于哪些场景?
- 实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。
- Pytorch seq2seq 模型架构实现英译法任务
- Redis - message publish and subscribe
- 期货开户是否有资金门槛?
猜你喜欢

MLX90640 红外热成像仪测温模块开发笔记(完整版)

路由策略

Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ

go笔记——map

BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合

Microsoft PC Manager V2.1 beta version officially released

Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)

c语言字符和字符串函数总结(二)

uni-app project summary

Redis - message publish and subscribe
随机推荐
好的期货公司开户让人省心省钱
Difference between JSP out.print() and out.write() methods
uni-app project summary
管理基础知识11
Day11 shell脚本基础知识
els block boundary deformation processing
管理基础知识17
go笔记之——goroutine
els block deformation judgment.
鲲鹏编译调试插件实战
What is Low-Code?What scenarios is low code suitable for?
这 4 款电脑记事本软件,得试试
Business test how to avoid missing?
Redis-消息发布订阅
请教一下本网站左下角的动漫人物是怎么做的?
go笔记——map
BGP first experiment
go笔记记录——channel
Redis 相关问题
Grid false data injection attacks detection based on coding strategy