装饰器模式(Decorator Pattern)

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构

譬如我们将把一个形状装饰上不同的颜色,同时又不改变形状类

优点:装饰模式和继承都是对功能的扩展,而装饰模式使用的是组合,可以不用继承而达到这一效果。使用过多的继承会增加系统的复杂性和偶合性

不使用类继承来扩展对象功能。 

缺点:装饰模式要产生一些辅助性的对象,但这些对象看上去都比较像,不是很容易检查(好的命名应该是提高检查的一个办法)

主要是解决:“过度地使用了继承来扩展对象的功能 ”

装饰模式和继承都是对功能的扩展,而装饰模式使用的是组合,使用过多的继承会增加系统的复杂性和偶合性。

 

http://www.runoob.com/design-pattern/decorator-pattern.html

http://www.iteye.com/topic/121149

http://www.jdon.com/designpatterns/decorator.htm   

 

我们读取文件时的调用:

Java代码 
  1. FileReader fr = new FileReader(filename);  
  2. BufferedReader br = new BufferedReader(fr);  

  

实际上Java 的I/O API就是使用Decorator模式实现的,I/O变种很多,如果都采取继承方法,将会产生很多子类,显然相当繁琐.

 

 

适配器模式:将一个类的接口,转换成客户期望的另外一个接口。适配器让原本接口不兼容的类可以很好的合作。 

装饰者模式:动态的将责任附加到对象上(因为利用组合而不是继承来实现,而组合是可以在运行时进行随机组合的)。若要扩展功能,装饰者提供了比继承更富有弹性的替代方案(同样地,通过组合可以很好的避免类暴涨,也规避了继承中的子类必须无条件继承父类所有属性的弊端)。

特点:

1. 装饰者和被装饰者拥有相同的超类型(可能是抽象类也可能是接口)

2. 可以用多个装饰类来包装一个对象,装饰类可以包装装饰类或被装饰对象

3. 因为装饰者和被装饰者拥有相同的抽象类型,因此在任何需要原始对象(被包装)的场合,都可以用装饰过的对象来替代它。

4. 装饰者可以在被装饰者的行为之前或之后,加上自己的附加行为,以达到特殊目的

5. 因为对象可以在任何的时候被装饰,所以可以在运行时动态地、不限量地用你喜欢的装饰者来装饰对象

 

PS:java.io库是最好的例子

小结:装饰者模式——动态地将责任附加到对象上。想要扩展功能,装饰者提供了有别于继承的另外一种选择。是一个很好的符合了开闭原则的设计模式。

  

总结:适配器模式主要是为了接口的转换,而装饰者模式关注的是通过组合来动态的为被装饰者注入新的功能或行为(即所谓的责任)。

适配器将一个对象包装起来以改变其接口;装饰者将一个对象包装起来以增强新的行为和责任;而外观将一群对象包装起来以简化其接口



 

步骤 1

创建一个接口。

Shape.java

public interface Shape {
   void draw();
}

 

步骤 2

创建实现接口的实体类。

public class Rectangle implements Shape {

   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}

 

public class Circle implements Shape {

   @Override
   public void draw() {
      System.out.println("Shape: Circle");
   }
}

 

步骤 3

创建实现了 Shape 接口的抽象装饰类。

ShapeDecorator.java

public abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;

   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }

   public void draw(){
      decoratedShape.draw();
   }	
}

 

步骤 4

创建扩展了 ShapeDecorator 类的实体装饰类。

RedShapeDecorator.java

public class RedShapeDecorator extends ShapeDecorator {

   public RedShapeDecorator(Shape decoratedShape) {
      super(decoratedShape);		
   }

   @Override
   public void draw() {
      decoratedShape.draw();	       
      setRedBorder(decoratedShape);
   }

   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}

 

步骤 5

使用 RedShapeDecorator 来装饰 Shape 对象。

DecoratorPatternDemo.java

public class DecoratorPatternDemo {
   public static void main(String[] args) {

      Shape circle = new Circle();

      Shape redCircle = new RedShapeDecorator(new Circle());

      Shape redRectangle = new RedShapeDecorator(new Rectangle());
      System.out.println("Circle with normal border");
      circle.draw();

      System.out.println("\nCircle of red border");
      redCircle.draw();

      System.out.println("\nRectangle of red border");
      redRectangle.draw();
   }
}

 

步骤 6

验证输出。

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red

 。。

 

 

猜你喜欢

转载自uule.iteye.com/blog/2385114