当前位置:网站首页>装饰者模式
装饰者模式
2022-08-10 17:36:00 【老蛙@】
咖啡问题
现需要完成一个咖啡下单的问题:
- 咖啡种类:Espresso、ShortBlack、LongBlack、Decaf
- 调料:Milk、Soy、Chocolate
- 要求在扩展新的咖啡种类时,具有良好的扩展性,改动方便
- 使用OO来计算不同种类咖啡的费用,客户可以点单品咖啡,也可以单品咖啡+调料组合
装饰者模式
基本介绍
- 装饰者模式能够动态的将新的功能附加到对象上,在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则
- 装饰者模式装饰者模式就像打包一个快递,其中包含主体(即打包的物品);以及包装(快递包装外壳);一个主体可以由多个包装嵌套装饰
装饰者模式解决咖啡问题

/*** * @author shaofan * @Description 装饰者模式解决咖啡问题 */
public class CoffeeBar {
public static void main(String[] args) {
// 一份牛奶,两份巧克力的Espresso
// 不带任何配料的咖啡,装饰者中的主体
Drink order = new Espresso();
// 加入一份牛奶,使用牛奶装饰主体
order = new Milk(order);
// 加入一份巧克力,使用巧克力再次装饰
for(int i=0;i<2;i++){
order = new Chocolate(order);
}
System.out.println(order.cost());
System.out.println(order.getDescription());
}
}
abstract class Drink{
private String desc;
private double price;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
abstract double cost();
abstract String getDescription();
}
class Decorator extends Drink{
private Drink drink;
public Decorator(Drink drink){
this.drink = drink;
}
@Override
double cost() {
return super.getPrice()+drink.cost();
}
@Override
String getDescription() {
return drink.getDescription()+"+"+super.getDesc();
}
}
class Coffee extends Drink{
@Override
double cost() {
return super.getPrice();
}
@Override
String getDescription() {
return getDesc();
}
}
class Espresso extends Coffee{
private final String DESC = "Espresso";
private final double PRICE = 5.0;
public Espresso(){
super.setDesc(DESC);
super.setPrice(PRICE);
}
}
class ShortBlack extends Coffee{
private final String DESC = "ShortBlack";
private final double PRICE = 5.0;
public ShortBlack(){
super.setDesc(DESC);
super.setPrice(PRICE);
}
}
class Chocolate extends Decorator{
private final String DESC = "Chocolate";
private final double PRICE = 1.0;
public Chocolate(Drink drink){
super(drink);
super.setDesc(DESC);
super.setPrice(PRICE);
}
}
class Milk extends Decorator{
private final String DESC = "Milk";
private final double PRICE = 2.0;
public Milk(Drink drink){
super(drink);
super.setDesc(DESC);
super.setPrice(PRICE);
}
}
源码分析
在Java的IO中,FilterInputStream就是一个装饰者

它的内部组合了一个InputStream,对FileInputStream、StrintgBufferInputStream、ByteArrayInputStream这些主体进行装饰,提供一些额外的功能
边栏推荐
- Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)
- 海思HI3516DV300开发资料
- 痛苦的四大原因
- 文档标题能否支持公式
- 验算移位距离和假设的通用性
- Making Pre-trained Language Models Better Few-Shot Learners
- mysql定义存储过程
- 【独立站运营】做社交媒体营销的两大关键点
- Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso「建议收藏」
- 忍不住 - 发个新帖子【为什么把红圈的功能入口隐藏?需要移动到鼠标到位置驻停才显示?】- 请投票
猜你喜欢

瑞虎8 PRO产品性能分析,一文读懂究竟何为“全域动力科技旗舰”

讯飞翻译机抢镜背后,跨语种沟通迈入全新时代

DGIOT平台实时展示OPC上报数据全流程代码剖析

基于AWS构建云上数仓第二步:AWS常见服务简介

Toronto Research Chemicals 双(乙酰丙酮)铂(II)

产品-Axure9英文版,A页面内a1状态跳转B页面的b2状态,(条件跳转状态)

「业务架构」业务能力的热图是什么,有啥用?

Live Review|How to build an enterprise-level cloud management platform in the multi-cloud era?(with the download of the construction guide)

事务的隔离级别,MySQL的默认隔离级别

网络层总结(未完待续)
随机推荐
leet面试150
「软件架构」10种常见的软件架构模式
DGIOT平台实时展示OPC上报数据全流程代码剖析
FFmpeg Huaping solution (modify source code, discard incomplete frames)
网络可观测性:让您的网络监控更上一层楼|TechGenix
BalsnCTF2021
华为云连续5年保持中国政务云基础设施市场份额第一…
HarmonyOS自动化测试框架—Hypium
Return the next higher prime number
多线程与高并发(11)——经典面试题之实现一个容器,提供两个方法,add,size。
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
Xilinx FPGA收发器参考时钟设计应用
R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化分组散点图、stat_mean函数在分组数据点外侧绘制凸包并突出显示分组均值点、自定会均值点的大小以及透明度
机器人控制器编程实践指导书旧版-实践八 机器人综合设计
老板加薪!看我做的WPF Loading!!!
本周四晚19:00知识赋能第六期第5课丨OpenHarmony WiFi子系统
Before opening a futures account, you must confirm the handling fee as soon as possible
五菱宏光MINI EV,唯一的缺点就是安全性
不止跑路,拯救误操作rm -rf /*的小伙儿
MongoDB教程