转载:http://chenhua-1984.iteye.com/blog/565629点击打开链接
记得以前听一个高手说java.io包采用了了装饰器模式,当时不是很明白什么事装饰器模式,现在,网上看了一下说明和帮助的例子,他们的例子是这样的。
定义
Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:
它必须具有一个装饰的对象。
它必须拥有与被装饰对象相同的接口。
它可以给被装饰对象添加额外的功能。
用一句话总结就是:保持接口,增强性能。
装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。
相同点:都拥有一个目标对象。
不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对象的接口。
源接口Sourcable.java
- public interface Sourcable {
- public void operation();
- }
源类Source.java
- public class Source implements Sourcable {
- public void operation() {
- System.out.println("原始类的方法");
- }
- }
- public class Decorator1 implements Sourcable {
- private Sourcable sourcable;
- public Decorator1(Sourcable sourcable){
- super();
- this.sourcable=sourcable;
- }
- public void operation() {
- System.out.println("第一个装饰器前");
- sourcable.operation();
- System.out.println("第一个装饰器后");
- }
- }
- public class Decorator2 implements Sourcable {
- private Sourcable sourcable;
- public Decorator2(Sourcable sourcable){
- super();
- this.sourcable=sourcable;
- }
- public void operation() {
- System.out.println("第二个装饰器前");
- sourcable.operation();
- System.out.println("第二个装饰器后");
- }
- }
- public class DecoratorTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Sourcable source = new Source();
- // 装饰类对象
- Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source)));
- obj.operation();
- }
- }