`
uule
  • 浏览: 6306332 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

装饰器模式(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

 。。

 

 

  • 大小: 50.8 KB
  • 大小: 50.8 KB
分享到:
评论

相关推荐

    java实现装饰器模式(Decorator Pattern)

    主要为大家详细介绍了java实现装饰器模式Decorator Pattern,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    C#装饰器模式(Decorator Pattern)实例教程

    主要介绍了C#装饰器模式(Decorator Pattern),以一个完整实例形式讲述了C#装饰器模式的实现过程,有助于深入理解C#程序设计思想,需要的朋友可以参考下

    详解java装饰模式(Decorator Pattern)

    主要为大家详细介绍了java装饰模式Decorator Pattern,这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装,对装饰器模式感兴趣的小伙伴们可以参考一下

    设计模式_装饰器模式.zip

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。 这种模式创建了一个装饰类,用来包装原有的类,并在...

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    13、装饰模式DECORATOR PATTERN 14、迭代器模式ITERATOR PATTERN 15、组合模式COMPOSITE PATTERN 16、观察者模式OBSERVER PATTERN 17、责任链模式 18、访问者模式VISITOR PATTERN 19、状态模式 20、原型模式 21...

    java 装饰模式(Decorator Pattern)详解及实例代码

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装

    C#版 24种设计模式

    备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...

    用Java实现23种设计模式

    装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command ...

    DecoratorPattern.rar

    内含装饰器模式的简单案例demo和用mindmaster绘制的脑图,方便java入门开发者进行java开发模式的学习。

    C#设计模式_设计模式_C#_

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令...

    C#设计模式(23种设计模式)

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令...

    33种JAVA设计模式DEMO

    装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3 行为型模式 这些设计模式特别关注对象之间的通信。 责任链模式(Chain of ...

    设计模式代码——c#

    8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型 13. 模板方法(Template Method) ...

    23种设计模式 (创建型,结构型,行为型)

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令...

    设计模式PPT

     装饰者模式(Decorator Pattern)  外观模式(Facade Pattern)  享元模式(Flyweight Pattern)  代理模式(Proxy Pattern) 行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,主要包含以下11种...

    Java设计模式,并加上个人理解

    1. 设计模式 1.1 含义 1.2 作用 1.3 设计原则 ...14. 迭代器模式 (Iterator Pattern) 15. 享元模式 (Flyweight Pattern) 16. 桥接模式 (Bridge Pattern) 17. 观察者模式 (Observer Pattern)

    DecoratorPattern:装饰者模式示例

    装饰模式装饰器设计模式的示例项目装饰器模式允许用户在不改变其结构的情况下向现有对象添加新功能。 这种类型的设计模式属于结构模式,因为这种模式充当现有类的包装器。 这种模式创建了一个装饰器类,它包装了原始...

    装饰器设计模式

    控制访问,装饰器设计的四个名词,装饰器模式与代理模式的区别,

    32种设计模式

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template ...

Global site tag (gtag.js) - Google Analytics