当前位置:网站首页>【DesignMode】装饰者模式(Decorator pattern)
【DesignMode】装饰者模式(Decorator pattern)
2022-07-05 23:57:00 【weixin_43224306】
基本介绍
装饰者模式:动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则(ocp)
这里提到的动态的将新功能附加到对象和 ocp 原则,在后面的应用实例上会以代码的形式体现,请同学们注意体会。
1.星巴克咖啡订单项目(咖啡馆):
咖啡种类/单品咖啡:Espresso(意大利浓咖啡)、ShortBlack、LongBlack(美式咖啡)、Decaf(无因咖啡)
调料:Milk、Soy(豆浆)、Chocolate
要求在扩展新的咖啡种类时,具有良好的扩展性、改动方便、维护方便
使用 OO 的来计算不同种类咖啡的费用: 客户可以点单品咖啡,也可以单品咖啡+调料组合。
2. 方案 1-解决星巴克咖啡订单项目
比较差的方案1
类图:
3. 方案 1-解决星巴克咖啡订单问题分析
Drink 是一个抽象类,表示饮料
des 就是对咖啡的描述, 比如咖啡的名字
cost() 方法就是计算费用,Drink 类中做成一个抽象方法. 4) Decaf 就是单品咖啡, 继承 Drink, 并实现 cost
Espres
边栏推荐
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- The difference of time zone and the time library of go language
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- [EF core] mapping relationship between EF core and C data type
- QT QPushButton details
- 哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
- FFMPEG关键结构体——AVFrame
- FFmpeg学习——核心模块
- Single merchant v4.4 has the same original intention and strength!
- Cloudcompare & PCL point cloud randomly adds noise
猜你喜欢

Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste

Spire Office 7.5.4 for NET

【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)

GD32F4xx uIP协议栈移植记录

认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)

Huawei equipment is configured with OSPF and BFD linkage

wx.getLocation(Object object)申请方法,最新版

时区的区别及go语言的time库
![[binary search tree] add, delete, modify and query function code implementation](/img/38/810a83575c56f17a7a0ed428a2e02e.png)
[binary search tree] add, delete, modify and query function code implementation

How to rotate the synchronized / refreshed icon (EL icon refresh)
随机推荐
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
FFmpeg学习——核心模块
【在线聊天】原来微信小程序也能回复Facebook主页消息!
Add noise randomly to open3d point cloud
GFS distributed file system
Laser slam learning record
15 MySQL-存储过程与函数
多普勒效应(多普勒频移)
[binary search tree] add, delete, modify and query function code implementation
用列表初始化你的vector&&initializer_list简介
跟着CTF-wiki学pwn——ret2libc1
总结了 800多个 Kubectl 别名,再也不怕记不住命令了!
Wechat applet -- wxml template syntax (with notes)
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
如何获取localStorage中存储的所有值
rsync远程同步
20. Migrate freetype font library
Shardingsphere source code analysis
7.5模拟赛总结
【luogu P3295】萌萌哒(并查集)(倍增)