当前位置:网站首页>【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
边栏推荐
- 20220703 week race: number of people who know the secret - dynamic rules (problem solution)
- 数据库遇到的问题
- How much do you know about the bank deposit business that software test engineers must know?
- 关于结构体所占内存大小知识
- 俄外交部:日韩参加北约峰会影响亚洲安全稳定
- Huawei equipment is configured with OSPF and BFD linkage
- Spire Office 7.5.4 for NET
- VBA fast switching sheet
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- 用列表初始化你的vector&&initializer_list简介
猜你喜欢

Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!

20. Migrate freetype font library

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

PADS ROUTER 使用技巧小记

Breadth first search open turntable lock

Senparc. Weixin. Sample. MP source code analysis

XML配置文件(DTD详细讲解)

How to get all the values stored in localstorage

Wechat applet -- wxml template syntax (with notes)

What are the functions of Yunna fixed assets management system?
随机推荐
开源crm客户关系统管理系统源码,免费分享
PADS ROUTER 使用技巧小记
14 MySQL view
Doppler effect (Doppler shift)
提升工作效率工具:SQL批量生成工具思想
Single merchant v4.4 has the same original intention and strength!
C # input how many cards are there in each of the four colors.
mysql-全局锁和表锁
Rsync remote synchronization
PV static creation and dynamic creation
[EF core] mapping relationship between EF core and C data type
Senparc.Weixin.Sample.MP源码剖析
Fiddler Everywhere 3.2.1 Crack
转:未来,这样的组织才能扛住风险
多普勒效应(多普勒频移)
GFS分布式文件系统
硬件及接口学习总结
shardingsphere源码解析
Huawei equipment is configured with OSPF and BFD linkage
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2