装饰模式又名包装(Wrapper)模式 
• 装饰模式以对客户端透明的方式扩展对象 的功能,是继承关系的一个替代方案 
• 装饰模式以对客户透明的方式动态的给一 个对象附加上更多的责任。换言之,客户 端并不会觉得对象在装饰前和装饰后有什么不同。
• 装饰模式可以在不创造更多子类的情况下, 将对象的功能加以扩展。
• 装饰模式把客户端的调用委派到被装饰类。 装饰模式的关键在于这种扩展完全是透明的。
• 装饰模式是在不必改变原类文件和使用继 承的情况下,动态的扩展一个对象的功能。 它是通过创建一个包装对象,也就是装饰 来包裹真实的对象。
装饰模式的角色: 
- 抽象构件角色(Component):给出一个抽 象接口,以规范准备接收附加责任的对象。 
 
- 具体构件角色(Concrete Component): 定义一个将要接收附加责任的类。 
 
- 装饰角色(Decorator):持有一个构件 (Component)对象的引用,并定义一个与 抽象构件接口一致的接口 
 
- 具体装饰角色(Concrete  Decorator):负责给构件对象“贴上”附加的责任。
 
装饰模式的特点:
继承
- 用来扩展一类对象的功能 
 
- 需要子类 
 
- 静态 
 
- 编译时分派职责
 
- 导致很多子类产生 
 
缺乏灵活性
实现自己的装饰模式
 
1 2 3 4 5
   | //抽象构件角色 public interface  Component { 	public void doSomthing() ; 	 }
   | 
 
1 2 3 4 5 6 7 8 9 10
   | //具体的构件角色 public class ConcreteComponent implements Component{
  	@Override 	public void doSomthing() { 		// TODO Auto-generated method stub 		System.out.println("功能A"); 	}
  }
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   | //装饰角色 public class Decorator implements Component{
  	//持有接口的引用 	private Component component; 	 	 	public Decorator(Component component ) { 		// TODO Auto-generated constructor stub 		this.component = component; 	} 	 	public Decorator() { 		// TODO Auto-generated constructor stub 	} 	 	 	 	@Override 	public void doSomthing() {
  		component.doSomthing(); 	}
  }
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | //具体的装饰角色 public class ConcreteDecorator extends Decorator {
  	public ConcreteDecorator(Component component) { 		super(component); 	}
  	@Override 	public void doSomthing() { 		super.doSomthing();
  		this.doAnotherThing(); 	}
  	private void doAnotherThing() { 		System.out.println("功能B"); 	}
  }
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | public class ConcreteDectator2 extends Decorator {
  	public ConcreteDectator2(Component component) { 		super(component); 	}
  	@Override 	public void doSomthing() { 		// TODO Auto-generated method stub 		super.doSomthing(); 		this.doAnotherThing(); 	}
  	private void doAnotherThing() { 		System.out.println("功能C"); 	} }
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   |  public class Client {
  	public static void main(String[] args) { //		//节点流 //		Component component = new ConcreteComponent(); //		 //		//过滤流 //		Component component2 = new ConcreteDecorator(component); //		 //		//过滤流 //		Component component3 = new ConcreteDectator2(component2); //		 //		component2.doSomthing(); //		System.out.println("======================="); //		component3.doSomthing(); 		 		 		Component component = new ConcreteDecorator(new ConcreteDectator2(new ConcreteComponent())); 		component.doSomthing(); 	} }
 
  | 
 
 这里我用了两种方式来测试,作用是一样的,只是与io流更相似。
我的csdn博客