定义: 动态的给一个对象增加其他职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更灵活。
Sitemesh框架就是应用了装饰模式,Sithmesh是一个非常优秀的页面修饰框架,使用它可以动态的给页面加一些装饰,可以完全的代替所有的include指令。
装饰模式中类和对象的关系为:
-
Component抽象构件:定义对象的接口,可以给这些对象动态增加职责(方法)
-
ConcreteComponent 具体构件:定义具体的对象,Decorator可以给它增加额外的职责(方法)
-
Decorator 装饰角色: 持有具体构件类的对象,以便执行原有功能
-
ConcreteDecorator具体装饰: 具体扩展的功能在这里
应用情景:
-
你想透明的并且动态的给对象增加心的职责(方法),而有不会影响其他的对象
-
你给对象增加的职责在未来会放生变化
下面就是一个简单的装饰模式的例子:以吃饭为例,现在要增加一些功能,在吃饭前加听音乐,吃饭后加唱歌(当然这也可以用动态代理来实现)
抽象构件
package com.lbx.component;
/*
* 抽象构件
*/
public interface Component {
public void eat();
}
具体构件
package com.lbx.conceteComponent;
import com.lbx.component.Component;
/**
* 具体构件
* @author Administrator
*
*/
public class ConcreteComponent implements Component {
public void eat() {
System.out.println("吃饭");
}
}
装饰角色
package com.lbx.decorator;
import com.lbx.component.Component;
/**
* 装饰角色
* @author Administrator
*
*/
public class Decorator implements Component {
private Component component;
protected Decorator(Component component) {
this.component = component;
}
@Override
public void eat() {
// TODO Auto-generated method stub
this.component.eat();
}
}
具体装饰1
package com.lbx.concreteDecorator;
import com.lbx.component.Component;
import com.lbx.decorator.Decorator;
/**
* 具体装饰(这里演示了两种扩展的情况,听音乐+吃饭和吃饭+唱歌)
* @author Administrator
*
*/
public class ConcreteDecoratorListen extends Decorator {
public ConcreteDecoratorListen(Component component) {
super(component);
}
public void eat(){
this.listen("听音乐"); //执行增加的功能
super.eat();
}
private void listen(Object obj){
System.out.println(obj);
}
}
具体装饰2
package com.lbx.concreteDecorator;
import com.lbx.component.Component;
import com.lbx.decorator.Decorator;
public class ConcreteDecoratorSing extends Decorator {
public ConcreteDecoratorSing(Component component) {
super(component);
// TODO Auto-generated constructor stub
}
public void eat() {
super.eat();
System.out.println(sing()); // 执行扩展功能
}
private String sing() {
return "唱歌";
}
}
测试方法
package com.lbx.test;
import com.lbx.component.Component;
import com.lbx.conceteComponent.ConcreteComponent;
import com.lbx.concreteDecorator.ConcreteDecoratorListen;
import com.lbx.concreteDecorator.ConcreteDecoratorSing;
public class Test {
public static void main(String[] args) {
Component component = new ConcreteComponent();
ConcreteDecoratorListen c = new ConcreteDecoratorListen(component);
c.eat();
ConcreteDecoratorSing c2 = new ConcreteDecoratorSing(component);
c2.eat();
}
}
分享到:
相关推荐
动态地给一个对象添加一些额外的职责。就扩展功能而言,它比生成子类方式更为灵活。
装饰者模式(Decorator Pattern)是一种结构型设计模式,它的定义是在不改变原有对象结构的基础上,动态地给该对象增加一些职责(即增加其额外功能)。这种模式允许向一个现有的对象添加新的功能,同时又不改变其...
Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现 VS2012 下通过
主要介绍了java 装饰模式(Decorator Pattern)详解的相关资料,需要的朋友可以参考下
设计模式—装饰者模式,实例代码演示,设计模式—装饰者模式,实例代码演示设计模式—装饰者模式,实例代码演示,
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) ...装饰模式(Decorator Pattern) 状态模式(State Pattern) 组合模式(Composite Pattern)
内含装饰器模式的简单案例demo和用mindmaster绘制的脑图,方便java入门开发者进行java开发模式的学习。
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装
13、装饰模式DECORATOR PATTERN 14、迭代器模式ITERATOR PATTERN 15、组合模式COMPOSITE PATTERN 16、观察者模式OBSERVER PATTERN 17、责任链模式 18、访问者模式VISITOR PATTERN 19、状态模式 20、原型模式 21...
主要为大家详细介绍了java装饰模式Decorator Pattern,这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装,对装饰器模式感兴趣的小伙伴们可以参考一下
主要介绍了C#装饰器模式(Decorator Pattern),以一个完整实例形式讲述了C#装饰器模式的实现过程,有助于深入理解C#程序设计思想,需要的朋友可以参考下
主要为大家详细介绍了java实现装饰器模式Decorator Pattern,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这是一个关于设计模式中的装饰者模式例子,C#,Vs205,Unity 5.6.3f1 (64-bit)有需要的请下载!本来不想设置积分,可是最低1分,没办法!
在软件系统中,有时候我们会使用继承来扩展对象的功能,但是由于继承为类型引入的静态特质,使得这种扩展方式...[GOF《设计模式》]图1Decorator模式结构图装饰模式动态地给一个对象添加额外的职责。不论一幅画有没有画
装饰者模式C++实现,简单示例
装饰模式装饰器设计模式的示例项目装饰器模式允许用户在不改变其结构的情况下向现有对象添加新功能。 这种类型的设计模式属于结构模式,因为这种模式充当现有类的包装器。 这种模式创建了一个装饰器类,它包装了原始...
DecoratorPattern 设计模式
装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command ...
装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令...