装饰模式
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
定义:动态给一个对象添加一些额外的职责,使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活。
设计初衷:通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。
装饰模式的结构
装饰模式使用原来被装饰的类的一个子类的实例,把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展是完全透明的。
装饰模式类图如下:
在装饰模式中的角色有:
● 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
● 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。
● 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
● 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。
源代码:
/** * @author: Col.Mai * @date:2014-9-23 下午11:07:34 * @description :<p>抽象角色构件</P> **/ public interface Component { void sampleOperation(); }
/** * @author: Col.Mai * @date:2014-9-23 下午11:08:40 * @description :<p>具体构件角色</P> **/ public class ConcreteComponent implements Component { public void sampleOperation() { //TODO 写相关业务代码 } }
/** * @author: Col.Mai * @date:2014-9-23 下午11:09:58 * @description :<p>装饰角色</P> **/ public class Decorator implements Component { private Component component; public void sampleOperation() { // 委派给构件 component.sampleOperation(); } public Decorator(Component component){ this.component = component; } }
/** * @author: Col.Mai * @date:2014-9-23 下午11:13:36 * @description :<p>具体装饰角色</P> **/ public class ConcreteDecorator extends Decorator { public ConcreteDecorator(Component component) { super(component); } @Override public void sampleOperation() { super.sampleOperation(); // 写相关的业务代码 } }
- 在上面的装饰类里,有一个私有的属性component,其数据类型是构件(Component)
- 此装饰类实现了构件(Component)接口
- 接口的方法也值得注意,每一个实现类的方法都委派给父类,但并不单纯地委派,而是有功能增强。
下面举个简单的例子,我记的我小时候看过一个动画片叫《战神金刚》,当时非常喜欢看,放学后就盯着电视连饭都不吃,为此没少挨揍,这个动画片讲的是几个小朋友团队合作组成战神金刚消灭强大的敌人,在组成战神金刚前,他们分别是单独的对象,在组成时他们分别组成身体、头部、腿部、手部从而完成战神金刚(时间久远具体剧情也忘记了,组成部分只是靠回忆猜测,不要在意这些细节,主要是以此为例)。
类图如下:
代码如下:
/** * @author: Col.Mai * @date:2014-9-26 上午12:39:24 * @description :<p>抽象构件角色(战神金刚)</P> **/ public interface Robot { /** * 组成某个部位 */ void composition(); }
/** * @author: Col.Mai * @date:2014-9-26 上午12:49:27 * @description :<p>具体构件角色</P> **/ public class ConcreteRobot implements Robot{ public void composition() { System.out.println("组成战神金刚!"); } }
/** * @author: Col.Mai * @date:2014-9-26 上午1:18:48 * @description :<p>装饰者角色</P> **/ public class Decorator implements Robot{ private Robot robot; public Decorator(Robot robot){ this.robot = robot; } public void composition() { robot.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:47:06 * @description :<p>装饰身体</P> **/ public class Decorator4Body extends Decorator { public Decorator4Body(Robot robot) { super(robot); } public void composition() { System.out.println("我来组成身体!"); super.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:47:28 * @description :<p>装饰手部</P> **/ public class Decorator4Hand extends Decorator { public Decorator4Hand(Robot robot) { super(robot); } public void composition() { System.out.println("我来组成手部!"); super.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:44:20 * @description : <p> 装饰头部 </P> **/ public class Decorator4Head extends Decorator { public Decorator4Head(Robot robot) { super(robot); } public void composition() { System.out.println("我来组成头部!"); super.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:46:27 * @description :<p>装饰腿部</P> **/ public class Decorator4Leg extends Decorator{ public Decorator4Leg(Robot robot) { super(robot); } public void composition() { System.out.println("我来组成腿部!"); super.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:55:34 * @description :<p>xxx功能描述</P> **/ public class Client { public static void main(String [] args){ Robot robot = new ConcreteRobot(); Robot obj = new Decorator4Body(new Decorator4Head(new Decorator4Leg(new Decorator4Hand(robot)))); obj.composition(); } }
我来组成身体! 我来组成头部! 我来组成腿部! 我来组成手部! 组成战神金刚!
装饰模式的简化
大多数情况下,装饰模式的实现都要比上面给出的示意性例子要简单。
有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。甚至在只有两个ConcreteDecorator类的情况下,都可以这样做。如下图所示:
还是组成《战神金刚》的例子,看我们简化后如何做。
类图如下:
源码:
/** * @author: Col.Mai * @date:2014-9-26 上午12:39:24 * @description :<p>抽象构件角色(战神金刚)</P> **/ public interface Robot { /** * 组成某个部位 */ void composition(); }
/** * @author: Col.Mai * @date:2014-9-26 上午12:49:27 * @description :<p>具体构件角色</P> **/ public class ConcreteRobot implements Robot{ public void composition() { System.out.println("组成战神金刚!"); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:47:06 * @description :<p>装饰身体</P> **/ public class Decorator4Body implements Robot { private Robot robot; public Decorator4Body(Robot robot) { this.robot = robot; } public void composition() { System.out.println("我来组成身体!"); robot.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:47:28 * @description :<p>装饰手部</P> **/ public class Decorator4Hand implements Robot { private Robot robot; public Decorator4Hand(Robot robot) { this.robot = robot; } public void composition() { System.out.println("我来组成手部!"); robot.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:44:20 * @description : <p> 装饰头部 </P> **/ public class Decorator4Head implements Robot { private Robot robot; public Decorator4Head(Robot robot) { this.robot = robot; } public void composition() { System.out.println("我来组成头部!"); robot.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:46:27 * @description :<p>装饰腿部</P> **/ public class Decorator4Leg implements Robot { private Robot robot; public Decorator4Leg(Robot robot) { this.robot = robot; } public void composition() { System.out.println("我来组成腿部!"); robot.composition(); } }
/** * @author: Col.Mai * @date:2014-9-26 上午12:55:34 * @description :<p>xxx功能描述</P> **/ public class Client { public static void main(String [] args){ Robot robot = new ConcreteRobot(); Robot obj = new Decorator4Body(new Decorator4Head(new Decorator4Leg(new Decorator4Hand(robot)))); obj.composition(); } }
输出
我来组成身体! 我来组成头部! 我来组成腿部! 我来组成手部! 组成战神金刚!
相关推荐
该文档描述了java装饰模式,装饰模式是javaIO流底层所采用的设计模式。
java 设计模式 装饰模式,希望对大家理解设计模式有帮助
java装饰模式,一个很强大的设计模式 直接解压运行Test.java
java中的装饰模式及动态代理模式示例源码,并且手写数据库连接池,以及展示动态代理模式在连接池中的使用
通过设计组装和升级大众CC来说明装饰模式,有一个VolkCC类实现Volk接口,设计一个CCDecorator实现Volk这个接口,在ConcreteCCDecorator这个具体装饰角色中进行功能的增加和重写。
装饰模式的示例代码及类图(用powerDesign画的),都是自己亲手写的,很简单,看不懂你来杀我! 注:oom文件需要手工用powerDesign打开,默认没有被PD关联。
设计模式--装饰者模式java例子
JAVA设计模式之装饰模式代码
项目经理接到一个项目,需完成编码,但完成编码前项目经理需先前期工作(需求分析、设计等)然后将编码工作委托给程序员,完成后项目经理做收尾工作
装饰模式.doc 装饰模式.doc 装饰模式.doc
java设计模式之装饰者模式代码
学习java 装饰模式(decorator)非常好的例子
本章可以称为“给爱用继承的人一个全新的设计眼界”。我们即将再度探讨典型滥用问题。你将在本章学到如何使用对象组合的方式,做到在运行时装饰类。为什么呢?一旦你熟悉了装饰者的技巧...——《Head First 设计模式》
主要为大家详细介绍了java装饰模式Decorator Pattern,这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装,对装饰器模式感兴趣的小伙伴们可以参考一下
java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式java设计模式之装饰模式
主要介绍了java 装饰模式(Decorator Pattern)详解的相关资料,需要的朋友可以参考下
博客原地址:http://blog.csdn.net/lhy_ycu/article/details/40031567
9 装饰模式 10 原型模式 11 委派模式 12 适配器模式 设计模式综合运用 1 门面+模版方法+责任链+策略 2 门面+模版方法+责任链+策略+工厂方法 3 动态代理+Spring AOP 4 责任链模式进阶 Spring Framework设计模式 ...
JAVA设计模式例程-装饰模式-的例程,看完以后基本上能对JAVA上实现装饰模式能有所了解,个人认为常各种模式并不是为了死套各种模式,而应该是灵活的在项目中结合实际情况而用各种模式的组合应用,呵呵!但前提是得对...
NULL 博文链接:https://alaric.iteye.com/blog/1911949