装饰者模式
来源:互联网 发布:mac壁纸轮播 编辑:程序博客网 时间:2024/06/08 02:23
装饰者模式
结合大话设计模式,对装饰者模式进行归纳整理
生活情景
比如物品在原有的功能基础上增加新的功能,比如手机开始只有接打电话发短信功能,然后给添加上播放音乐功能,再添加上播放视频功能,在添加上蓝牙功能等等。
装饰者定义
装饰者模式,动态地给一个对象添加额外的职责,增加功能而言,装饰者模式比生成子类更加灵活(需要体会)。
换句话说,就是为已有功能动态地添加更多的功能 。
装饰者模式解说
当系统增加新功能的时候,是向旧的类中添加新的代码。这些新代码通常装饰了原有类的核心职责或行为。装饰者模式优点在于把类中装饰的功能从类中移除掉了,有效地把类的核心职责和装饰功能分开。
基础功能接口
package com.neutron.decorate;/** * 装饰者模式对象接口 * 职责:给对象动态添加职责 * * * @author neutron * */public interface Component {void operate();}
装饰者抽象类
package com.neutron.decorate;import java.util.Objects;/** * 装饰者抽象类 * * 职责: * 实现Component接口,从外扩展Component接口功能 * 对于Component接口,不需要知道装饰者类Decrator * 关系: * 1.Decrator继承Component * 2.Decrator和Component聚合关系 * * @author neutron * */public abstract class Decrator implements Component {protected Component component;public Decrator(Component component) {Objects.requireNonNull(component, "component can't be null");this.component = component;}@Overridepublic void operate() {this.component.operate();}}
被装饰者类
package com.neutron.decorate;/** * 动态添加功能的对象,被装饰者 * * @author neutron * */public class RealComponent implements Component {@Overridepublic void operate() {System.err.println("be decorated base called");}}
实际装饰者类
package com.neutron.decorate;public class DecratorA extends Decrator {public DecratorA(Component component) {super(component);}@Overridepublic void operate() {super.operate();setBehavior();}public void setBehavior() {System.err.println("DecratorA 特有的方法");}}
package com.neutron.decorate;public class DecratorB extends Decrator {public DecratorB(Component component) {super(component);}@Overridepublic void operate() {super.operate();setBehavior();}public void setBehavior() {System.err.println("DecratorB 特有的方法");}}
装饰者客户端
package com.neutron.entity;import com.neutron.decorate.Decrator;import com.neutron.decorate.DecratorA;import com.neutron.decorate.DecratorB;import com.neutron.decorate.RealComponent;/** * 装饰者模式客户端 * * 从功能角度说: * decratorA装饰decorated,即decratorA拥有decorated的功能 * decratorB装饰decratorA,即decratorB拥有decratorA和decorated的功能 * * @author neutron * */public class DecoraterClent {public static void main(String[] args) {// 1.打算给RealComponent进行各种装饰,即动态添加各种功能RealComponent decorated = new RealComponent();// 2.使用DecratorA装饰decoratedDecrator decratorA = new DecratorA(decorated);// 3.使用DecratorB装饰decratorADecrator decratorB = new DecratorB(decratorA);decratorB.operate();}}
说明:使用什么方式进行装饰比较。
1.使用set方式进行方法设置,那么方便拓展,便于开发
2.使用构造方法模式进行拓展,比如上面客户端中使用构造方法中参数形式,更适合固定设置,比如API。
0 0
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- Leetcode 213. House Robber II
- Leetcode 47. Permutations II
- 快速排序算法浅析
- Leetcode 46. Permutations
- Java_每天学习一点点之Java开发实战经典第三章习题
- 装饰者模式
- CKEditor+CKFinder服务器临时文件存储路径的问题
- 如何修改virtualBox下Ubuntu的虚拟内存
- 当jsp页面完全加载完成后执行一个js函数
- 339. Nested List Weight Sum
- 循环队列的实现(C语言版)
- eclipse之导入项目
- 迅达地理平台说明及最新动态
- rhel 6.5 yum源的配置