当前位置: 首页 > news >正文

设计模式-第4章(装饰模式)

装饰模式

  • 装饰模型
  • 装饰模式示例
  • 商场收银程序(简单工厂+策略+装饰模式实现)
  • 装饰模式总结

装饰模型

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
在这里插入图片描述
Component 是定义一个对象接口,可以给这些对象动态地添加职责。
ConcreateComponent 是定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator,装饰抽象类,继承了 Component,从外类来扩展 Component 类的功能,但对应Component 来说,是无须知道 Decorator的存在。
ConcreateDecorator 就是具体的装饰对象,起到给 Component 添加职责的功能。

// Component类
abstract class Component {public abstract void Operation();
}
// ConcreteComponent类
class ConcreteComponent extends Component {public void Operation() {System.out.println("具体对象的实际操作");}
}
//Decorator类
abstract class Decorator extends Component {protected Component component;//装饰一个Component对象public void SetComponent(Component component) {this.component = component;}//重写Operation(),实际调用component的Operation方法public void Operation() {if (component != null) {component.Operation();}}
}
//ConcreteDecoratorA类
class ConcreteDecoratorA extends Decorator {//本类独有子段,以区别于ConcreteDecoratorB类private String addedState;public void Operation() {//首先运行了原有Component的Operation()super.Operation();this.addedState = "具体装饰对象A的独有操作";//再执行本类独有功能System.out.println(this.addedState);}
}
//ConcreteDecoratorB类
class ConcreteDecoratorB extends Decorator {public void Operation() {//首先运行了原有Component的Operation()super.Operation();//再执行本类独有功能this.AddedBehavior();}//本类独有方法,以区别于ConcreteDecoratorA类private void AddedBehavior() { System.out.println("具体装饰对象B的独有操作");}
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		ConcreteComponent c = new ConcreteComponent();ConcreteDecoratorA d1 = new ConcreteDecoratorA();ConcreteDecoratorB d2 = new ConcreteDecoratorB();//首先用d1来包装c,扩展对象c的方法d1.SetComponent(c);	//再用有来包装d1,扩展对象d1的方法d2.SetComponent(d1);//最终执行d2的Operation()d2.Operation();   	System.out.println();System.out.println("**********************************************");}
}

装饰模式利用SetComponent来对对象进行包装,这样每个装饰对象的实现和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链中。

如果只有一个ConcreteComponent 类而没有抽象的 Component 类,那么 Decorator 类可以是 ConcreteComponent 的一个子类。
同样道理,如果只要一个 ConcreteDecorator 类,那么就没有必要建立一个单独的 Decorator 类,而可以把 Decorator 和 ConcreteDecorator 的责任合并成一个类。

装饰模式示例

在这里插入图片描述
人物形象接口

public interface ICharacter {public void show();
}

具体人类

public class Person implements ICharacter {private String name;public Person(String name) {this.name = name;}public void show() {System.out.println("装扮的"+name);}
}

服饰类

public class Finery implements ICharacter {protected ICharacter component;public void decorate(ICharacter component) {this.component=component;}public void show() {if (this.component != null){this.component.show();}}
}

具体服饰类(ConcreteDecorator)

public class LeatherShoes extends Finery {public void show(){System.out.print(" 皮鞋");super.show();}
}
public class BigTrouser extends Finery {public void show(){System.out.print(" 垮裤");super.show();}
}
public class TShirts extends Finery {public void show(){System.out.print(" 大T恤");super.show();}
}
public class Tie extends Finery {public void show(){System.out.print(" 领带");super.show();}
}
public class Suit extends Finery {public void show(){System.out.print(" 西装");super.show();}
}
public class Strawhat extends Finery {public void show(){System.out.print(" 草帽");super.show();}
}
public class Sneakers extends Finery {public void show(){System.out.print(" 球鞋");super.show();}
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		Person xc = new Person("小菜");System.out.println(" 第一种装扮:");Sneakers pqx = new Sneakers();      //生成球鞋实例pqx.decorate(xc);                   //球鞋装饰小菜BigTrouser kk = new BigTrouser();   //生成垮裤实例kk.decorate(pqx);                   //垮裤装饰“有球鞋装饰的小菜”TShirts dtx = new TShirts();        //生成T恤实例dtx.decorate(kk);                   //T恤装饰“有垮裤球鞋装饰的小菜”dtx.show();                         //执行形象展示System.out.println(" 第二种装扮:");LeatherShoes px = new LeatherShoes();//生成皮鞋实例px.decorate(xc);                    //皮鞋装饰小菜Tie ld = new Tie();                 //生成领带实例ld.decorate(px);                    //领带装饰“有皮鞋装饰的小菜”Suit xz = new Suit();               //生成西装实例xz.decorate(ld);                    //西装装饰“有领带皮鞋装饰的小菜”xz.show();                          //执行形象展示System.out.println(" 第三种装扮:");Sneakers pqx2 = new Sneakers();     //生成球鞋实例pqx2.decorate(xc);                  //球鞋装饰小菜LeatherShoes px2 = new LeatherShoes();//生成皮鞋实例px2.decorate(pqx2);                 //皮鞋装饰“有球鞋装饰的小菜”BigTrouser kk2 = new BigTrouser();  //生成垮裤实例kk2.decorate(px2);                  //垮裤装饰“有皮鞋球鞋装饰的小菜”Tie ld2 = new Tie();                //生成领带实例ld2.decorate(kk2);                  //领带装饰“有垮裤皮鞋球鞋装饰的小菜”Strawhat cm2 = new Strawhat();      //生成草帽实例cm2.decorate(ld2);                  //草帽装饰“有领带垮裤皮鞋球鞋装饰的小菜”cm2.show();                         //执行形象展示System.out.println();System.out.println("**********************************************");}
}

商场收银程序(简单工厂+策略+装饰模式实现)

在这里插入图片描述
ISale接口:用作装饰模式里的 Component。
CashNormal: 用作装饰模式里的 ConcreteComponent。
CashSuper: 用作装饰模式里的 Decorator 。
CashReturn:用作装饰模式里的 ConcreateDecorator 。

public interface ISale {public double acceptCash(double price,int num);
}
// 正常收费,原价返回
public class CashNormal implements ISale {public double acceptCash(double price,int num){return price * num;}
}
public class CashSuper implements ISale {protected ISale component;//装饰对象public void decorate(ISale component) {this.component=component;}public double acceptCash(double price,int num){var result = 0d;if (this.component != null){//若装饰对象存在,则执行装饰的算法运算result = this.component.acceptCash(price,num);    }return result;}
}
public class CashRebate extends CashSuper {private double moneyRebate = 1d;//打折收费。初始化时必需输入折扣率。八折就输入0.8public CashRebate(double moneyRebate){this.moneyRebate = moneyRebate;}//计算收费时需要在原价基础上乘以折扣率public double acceptCash(double price,int num){double result = price * num * this.moneyRebate;return super.acceptCash(result,1);}
}public class CashReturn extends CashSuper {private double moneyCondition = 0d; //返利条件private double moneyReturn = 0d;    //返利值//返利收费。初始化时需要输入返利条件和返利值。//比如“满300返100”,就是moneyCondition=300,moneyReturn=100public CashReturn(double moneyCondition,double moneyReturn){this.moneyCondition = moneyCondition;this.moneyReturn = moneyReturn;}//计算收费时,当达到返利条件,就原价减去返利值public double acceptCash(double price,int num){double result = price * num;if (moneyCondition>0 && result >= moneyCondition)result = result - Math.floor(result / moneyCondition) * moneyReturn; return super.acceptCash(result,1);   }   
}
public class CashContext {private ISale cs;   //声明一个ISale接口对象    //通过构造方法,传入具体的收费策略public CashContext(int cashType){switch(cashType){case 1:this.cs = new CashNormal();break;case 2:this.cs = new CashRebate(0.8d);break;case 3:this.cs = new CashRebate(0.7d);break;case 4:this.cs = new CashReturn(300d,100d);break;case 5://先打8折,再满300返100// 装饰模式CashNormal cn = new CashNormal();CashReturn cr1 = new CashReturn(300d,100d); CashRebate cr2 = new CashRebate(0.8d);cr1.decorate(cn);   //用满300返100算法包装基本的原价算法cr2.decorate(cr1);  //打8折算法装饰满300返100算法this.cs = cr2;      //将包装好的算法组合引用传递给cs对象break;case 6://先满200返50,再打7折// 装饰模式CashNormal cn2 = new CashNormal();CashRebate cr3 = new CashRebate(0.7d);CashReturn cr4 = new CashReturn(200d,50d); cr3.decorate(cn2);  //用打7折算法包装基本的原价算法cr4.decorate(cr3);  //满200返50算法装饰打7折算法this.cs = cr4;      //将包装好的算法组合引用传递给cs对象break;}}public double getResult(double price,int num){// 根据收费策略的不同,获得计算结果return this.cs.acceptCash(price,num);}    
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		int discount = 0; 		//商品折扣模式double price = 0d; 		//商品单价int num = 0;			//商品购买数量double totalPrices = 0d;//当前商品合计费用double total = 0d;		//总计所有商品费用Scanner sc = new Scanner(System.in);do {System.out.println("商品折扣模式如下:");	System.out.println("1.正常收费");	System.out.println("2.打八折");	System.out.println("3.打七折");	System.out.println("4.满300送100");	System.out.println("5.先打8折,再满300送100");	System.out.println("6.先满200送50,再打7折");	System.out.println("请输入商品折扣模式:");	discount = Integer.parseInt(sc.nextLine());System.out.println("请输入商品单价:");	price = Double.parseDouble(sc.nextLine());System.out.println("请输入商品数量:");	num = Integer.parseInt(sc.nextLine());System.out.println();	if (price>0 && num>0){//根据用户输入,将对应的策略对象作为参数传入CashContext对象中CashContext cc = new CashContext(discount);//通过Context的getResult方法的调用,可以得到收取费用的结果//让具体算法与客户进行了隔离totalPrices = cc.getResult(price,num);total = total + totalPrices;System.out.println();	System.out.println("单价:"+ price + "元 数量:"+ num +" 合计:"+ totalPrices +"元");	System.out.println();System.out.println("总计:"+ total+"元");	System.out.println();}}while(price>0 && num>0);System.out.println();System.out.println("**********************************************");}
}

装饰模式总结

装饰模式是为已有功能动态地添加更多功能的一种方式。

当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为,但这种做法的问题在于,它们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度。
装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

把类中的装饰功能从类中搬移去除,这样可以简化原有的类。这样做的好处就是有效地把类的核心职责和装饰功能区分开了。

相关文章:

设计模式-第4章(装饰模式)

装饰模式装饰模型装饰模式示例商场收银程序(简单工厂策略装饰模式实现)装饰模式总结装饰模型 装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为…...

【算法设计-分治】快速幂与龟速乘

文章目录1. 快速幂2. 龟速乘3. 快速幂取模4. 龟速乘取模5. 快速幂取模优化1. 快速幂 算法原理: 计算 311: 311 (35)2 x 335 (32)2 x 332 3 x 3仅需计算 3 次,而非 11 次 计算 310: 310 (35)235 (32)2 x 332 3 x 3仅需计算…...

基于新一代kaldi项目的语音识别应用实例

本文是由郭理勇在第二届SH语音技术研讨会和第七届Kaldi技术交流会上对新一代kaldi项目在学术及“部署”两个方面报告的内容上的整理。如果有误,欢迎指正。 文字整理丨李泱泽 编辑丨语音小管家 喜报:新一代Kaldi团队三篇论文均被语音顶会ICASSP-2023接…...

【GO】31.grpc 客户端负载均衡源码分析

这篇文章是记录自己查看客户端grpc负载均衡源码的过程,并没有太详细的讲解,参考价值不大,可以直接跳过,主要给自己看的。一.主要接口:Balancer Resolver1.Balancer定义Resolver定义具体位置为1.grpc源码对解析器(resol…...

PTA L1-058 6翻了(详解)

前言:内容包括:题目,代码实现,大致思路,代码解读 题目: “666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”&#xff0…...

【Origin科研绘图】如何快速绘制一个折线图 ||【前端特效】爱心篇 之 幸好有你 || 泰坦尼克号——乘客生存与否 预测 || PyCharm使用介绍

🎯作者主页:追光者♂ 🌸个人简介:在读计算机专业硕士研究生、CSDN-人工智能领域新星创作者🏆、2022年CSDN博客之星人工智能领域TOP4🌟、阿里云社区专家博主🏅 【无限进步,一起追光!】 🍎欢迎点赞👍 收藏⭐ 留言📝 🌿本篇,首先是:基于科研绘图工具O…...

一文解读电压放大器(电压放大器原理)

关于电压放大器的科普知识,之前讲过很多,今天为大家汇总一篇文章来详细的讲解电压放大器,希望大家对于电压放大器能有更清晰的认识。电压放大器是什么:电压放大器是一种常用的电子器件,它的主要作用是把输入信号的振幅…...

线上监控诊断神器arthas

目录 什么是arthas 常用命令列表 1、dashboard仪表盘 2、heapdump dumpJAVA堆栈快照 3、jvm 4、thread 5、memory 官方文档 安装使用 1、云安装arthas 2、获取需要监控进程ID 3、运行arthas 4、进入仪表盘 5、其他命令使用查看官方文档 什么是arthas arthas是阿…...

@Import注解的原理

此注解是springboot自动注入的关键注解,所以拿出来单独分析一下。 启动类的run方法跟进去最终找到refresh方法; 这里直接看这个org.springframework.context.support.AbstractApplicationContext#refresh方法即可,它下面有一个方法 invoke…...

平台总线开发(id和设备树匹配)

目录 一、ID匹配之框架代码 二、ID匹配之led驱动​​​​​​​ 三、设备树匹配 四、设备树匹配之led驱动 五、一个编写驱动用的宏 一、ID匹配之框架代码 id匹配(可想象成八字匹配):一个驱动可以对应多个设备 ------优先级次低 注意事项…...

TS泛型,原来就这?

一、泛型是什么?有什么作用? 当我们定义一个变量不确定类型的时候有两种解决方式: 使用any 使用any定义时存在的问题:虽然知道传入值的类型但是无法获取函数返回值的类型;另外也失去了ts类型保护的优势 使用泛型 泛型…...

关于算法学习和刷题的建议

大家好,我是方圆。最近花时间学了学算法,应该算是我接触Java以来第一次真正的学习它,这篇帖子我会说一些我对算法学习的理解,当然这仅仅是浅浅的入算法的门,如果想深挖或者是有基础的人想提升自己,我觉得这…...

2023年“网络安全”赛项浙江省金华市选拔赛 任务书

2023年“网络安全”赛项浙江省金华市选拔赛 任务书 任务书 一、竞赛时间 共计3小时。 二、竞赛阶段 竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值 第一阶段单兵模式系统渗透测试 任务一 Windows操作系统渗透测试 任务二 Linux操作系统渗透测试 任务三 网页渗透 任务四 Linux系统…...

http协议简介

http 1.简介 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。1960年美国人Ted Nelson构思了一种通过计算机处…...

CSDN 第三十一期竞赛题解

第二次参加 总分77.5,主要是在最后一题数据有误,花费了巨量时间… 参加的另一次比赛最后一道题目也出现了一点问题,有点遗憾。 题解 T1:最优利润值 你在读的经营课程上,老师布置了一道作业。在一家公司的日常运营中&…...

EM_ASM系列宏定义(emscripten)

2.5 EM_ASM系列宏很多编译器支持在C/C代码直接嵌入汇编代码,Emscripten采用类似的方式,提供了一组以“EM_ASM”为前缀的宏,用于以内联的方式在C/C代码中直接嵌入JavaScript代码。2.5.1 EM_ASMEM_ASM使用很简单,只需要将欲执行的Ja…...

Batchnorm和Layernorm的区别

在深度学习训练中,我们经常会遇到这两个归一化操作,他们之间有什么区别呢?我们来简单介绍一下: BatchNorm: 在深度学习训练的时候我们的数据如果没有经过预处理,有可能会出现梯度消失或者梯度爆炸的情况&…...

高级前端面试题汇总

iframe 有那些优点和缺点? iframe 元素会创建包含另外一个文档的内联框架(即行内框架)。 优点: 用来加载速度较慢的内容(如广告)可以使脚本可以并行下载可以实现跨子域通信 缺点: iframe 会…...

HTML#5表单标签

一. 表单标签介绍表单: 在网页中主要负责数据采集功能,使用<form>标签定义表单表单项: 不同类型的input元素, 下拉列表, 文本域<form> 定义表单<input> 定义表单项,通过typr属性控制输入形式<label> 为表单项定义标注<select> 定义下拉列表<o…...

ONNX可视化与编辑工具

ONNX可视化与编辑工具netrononnx-modifier在模型部署的过程中&#xff0c;需要使用到ONNX模型&#xff0c;下面给大家推荐两个ONNX可视化与编辑工具&#xff0c;其中&#xff0c;netron仅支持模型的可视化&#xff0c;onnx-modifier支持ONNX的可视化与编辑。 netron Netron是…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

阿里云ACP云计算备考笔记 (5)——弹性伸缩

目录 第一章 概述 第二章 弹性伸缩简介 1、弹性伸缩 2、垂直伸缩 3、优势 4、应用场景 ① 无规律的业务量波动 ② 有规律的业务量波动 ③ 无明显业务量波动 ④ 混合型业务 ⑤ 消息通知 ⑥ 生命周期挂钩 ⑦ 自定义方式 ⑧ 滚的升级 5、使用限制 第三章 主要定义 …...

相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解

【关注我&#xff0c;后续持续新增专题博文&#xff0c;谢谢&#xff01;&#xff01;&#xff01;】 上一篇我们讲了&#xff1a; 这一篇我们开始讲&#xff1a; 目录 一、场景操作步骤 二、日志基础关键字分级如下 三、场景日志如下&#xff1a; 一、场景操作步骤 操作步…...

相机从app启动流程

一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...

【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制

使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下&#xff0c;限制某个 IP 的访问频率是非常重要的&#xff0c;可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案&#xff0c;使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...

Vite中定义@软链接

在webpack中可以直接通过符号表示src路径&#xff0c;但是vite中默认不可以。 如何实现&#xff1a; vite中提供了resolve.alias&#xff1a;通过别名在指向一个具体的路径 在vite.config.js中 import { join } from pathexport default defineConfig({plugins: [vue()],//…...

若依登录用户名和密码加密

/*** 获取公钥&#xff1a;前端用来密码加密* return*/GetMapping("/getPublicKey")public RSAUtil.RSAKeyPair getPublicKey() {return RSAUtil.rsaKeyPair();}新建RSAUti.Java package com.ruoyi.common.utils;import org.apache.commons.codec.binary.Base64; im…...

AD学习(3)

1 PCB封装元素组成及简单的PCB封装创建 封装的组成部分&#xff1a; &#xff08;1&#xff09;PCB焊盘&#xff1a;表层的铜 &#xff0c;top层的铜 &#xff08;2&#xff09;管脚序号&#xff1a;用来关联原理图中的管脚的序号&#xff0c;原理图的序号需要和PCB封装一一…...