The Decorator Pattern of Java Design Patterns

Reprinted: https://www.cnblogs.com/zhangtianq/p/6091047.html


Responsibilities: Dynamically add new functionality to an object

   The Decorator pattern is a technique used in place of inheritance to extend new functionality of an object without adding subclasses through inheritance. Use the association relationship of objects instead of inheritance, which is more flexible and avoids the rapid expansion of the type system.


Implementation details:

- Component abstract component role: real objects and decorative objects have the same interface. In this way, client objects can interact with decorated objects in the same way as real objects.

——ConcreteComponent specific component role (real object): FileInputStream, FileOutputStream in the io stream

- Decorator role: holds a reference to an abstract component. The decorated object accepts all client requests and forwards those requests to the real object. This way, new functionality can be added before and after the real object is called.

- ConcreteDecorator specific decoration role: responsible for adding new responsibilities to component objects.


 Use scenarios in development:

  Design of input stream and output stream in IO

  Graphical interface component function in Swing package

  The Servlet API provides a default implementation class of the Decorator design pattern for a request object, HttpServletRequestWrapper. The HttpServletRequestWrapper class enhances the function of the request object.

  In Struts2, the processing of request, response, and session objects


 

1. Create an abstract component ICar interface, and create specific construction roles and specific decoration roles

copy code
1  package com.ztq.decorator;
 2  
3  /** *
 4  * Abstract component
 5  * @author ZTQ
 6  *
 7   */ 
8  public  interface ICar {
 9      void move();
 10  }
 11  
12  // ConcreteComponent concrete component role ( real object) 
13  class Car implements ICar{
 14  
15      @Override
 16      public  void move() {
 17          System.out.println("Run on land!" );
18     }
19     
20 }
21 
22 class SuperCar implements ICar{
23     private ICar car;
24     public SuperCar(ICar car){
25         this.car = car;
26     }
27     
28     @Override
29     public void move() {
30         car.move();
31     }
32     
33 }
34 
35 //ConcreteDecorator具体装饰角色
36 class FlyCar extendsSuperCar{
 37  
38      public FlyCar(ICar car) {
 39          super (car);
 40      }
 41      
42      public  void fly(){
 43          System.out.println("Fly in the sky" );
 44      }
 45      
46      @Override
 47      public  void move () {
 48          super .move();
 49          fly();
 50      }
 51  }
 52  
53  // ConcreteDecorator concrete decoration role 
54  class WaterCar extends SuperCar{
55 
56     public WaterCar(ICar car) {
57         super(car);
58     }
59     
60     public void swim(){
61         System.out.println("水里游");
62     }
63     
64     @Override
65     public void move() {
66         super.move();
67         swim();
68     }
69 }
70 
71 //ConcreteDecorator具体装饰角色
72 class AICar extends SuperCar{
73 
74     public AICar(ICar car) {
75         super(car);
76     }
77     
78     public void autoMove(){
79         System.out.println("自动跑");
80     }
81     
82     @Override
83     public void move() {
84         super.move();
85         autoMove();
86     }
87 }
copy code

 

2. Create a test class Client

copy code
 1 package com.ztq.decorator;
 2 
 3 public class Client {
 4     public static void main(String[] args) {
 5         Car car = new Car();
 6         car.move();
 7         
 8         System.out.println("增加新的功能:飞行");
 9         FlyCar flycar = new FlyCar(car);
10         flycar.move();
11         
12         System.out.println("增加新的功能:水里游");
13         WaterCar waterCar = new WaterCar(car);
14         waterCar.move();
15         
16         System.out.println("增加两个新的功能,飞行,水里游");
17         WaterCar waterCar2 = new WaterCar(new FlyCar(car));
18         waterCar2.move();
19     }
20 }
copy code

 

结果:

陆地上跑!
增加新的功能:飞行
陆地上跑!
天上飞
增加新的功能:水里游
陆地上跑!
水里游
增加两个新的功能,飞行,水里游
陆地上跑!
天上飞
水里游

 

UML图:

 


 

总结:

  装饰模式(Decorator)也叫包装器模式(Wrapper)

  装饰模式降低系统的耦合度,可以动态的增加或删除对象的职责,并使得需要装饰的具体构建类和具体装饰类可以独立变化,以便增加新的具体构建类和具体装饰类。

  优点:

  • 扩展对象功能,比继承灵活,不会导致类个数急剧增加
  • 可以对一个对象进行多次装饰,创造出不同行为的组合,得到功能更加强大的对象
  • 具体构建类和具体装饰类可以独立变化,用户可以根据需要自己增加新的具体构件子类和具体装饰子类

  缺点:

  • produces a lot of small objects. A large number of small objects occupy memory, which affects performance to a certain extent
  • The decoration mode is prone to errors, and debugging and troubleshooting are more troublesome

 

The difference between decoration mode and bridge mode:

Both patterns are designed to solve the problem of too many subclassed objects. But their incentives are different. The bridge mode is that the existing mechanism of the object itself changes along multiple dimensions, and the existing part is unstable. Decorative mode is for adding new functionality.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325810447&siteId=291194637