当前位置:网站首页>建造者模式
建造者模式
2022-06-25 11:09:00 【tlqwanttolearnit】
建造者模式(Builder Pattern)又叫生成器模式,有点类似工厂模式,两者都是用专门的类来实现对象的创建。
- 工厂模式用来创建一系列相关类型但不同的对象。
- 建造者模式则用来创建一种类型的复杂对象,通过设置不同的可选参数,定制化创建不同对象。
主要区别是建造者模式着重一步步构造一个复杂对象,而抽象工厂模式着重于多个系列的产品对象。
为什么使用建造者模式
- 一个类有很多属性,构造函数和参数过于长。
- 属性之间有一定的依赖和约束关系。
- 对象的创建需要遵循一定的顺序或者步骤
有一句话对建造者模式总结的很好:
当我们想要创建一个由多个部分构成的对象,而且他们的构建需要一步接一步的地完成,只有当各个部分都创建好,这个对象才算完整。
这正是 建造者设计模式的用武之地。
建造者模式的优缺点
优点
- 建造者独立,易扩展。
- 便于控制细节风险。
- 将对象的构造与表现解耦
缺点
- 产品必须有共同点,范围有限制。
- 如果内部变化复杂,会有很多的建造类。
建造者模式的构成
- 实际对象:实际要返回的对象
- 建造者抽象类:定义建造者实体类需要实现的功能。
- 建造者实体类:继承建造者抽象类,具体实现抽象类方法。
- 指挥者类:
边栏推荐
- Ladder Side-Tuning:预训练模型的“过墙梯”
- 金仓数据库 KingbaseES 插件ftutilx
- 金仓KFS数据集中场景(多对一)部署
- 2022 PMP project management examination agile knowledge points (2)
- Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
- c盘使用100%清理方法
- 每日3题(2)- 找出数组中的幸运数
- 【文件包含漏洞-03】文件包含漏洞的六种利用方式
- Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
- 撸一个随机数生成器
猜你喜欢

中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析

Use of three-level linkage plug-ins selected by provinces and cities

Double tampon transparent cryptage et décryptage basé sur le cadre minifilter

ZABBIX distributed system monitoring

Garbage collection mechanism

数据库系列:MySQL索引优化总结(综合版)

金仓KFS数据级联场景部署

zabbix分布式系统监控

仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)

Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
随机推荐
How to start the phpstudy server
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
Application of global route guard
Continuous delivery jenkinsfile syntax
网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
【上云精品】节能提效!加速纺织业“智造”转型
金仓数据库 KingbaseES 插件force_view
Remove the problem of orange border on the desktop control in WebView
Open source invites you to participate in the openssf Open Source Security Online Seminar
Big endian and little endian
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
SystemVerilog (XIII) - enumerate data types
金仓KFS数据级联场景部署
[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
1-7snapshots and clones in VMWare
每日3题(3)-检查整数及其两倍数是否存在
Upload and modify the use of avatars
Server rendering
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析