[Decorator Decorator]

Decorator belonging structural model, it is packaged as a conventional class

 

1, intentions:

  To an object dynamically add some additional responsibilities. Increased functionality, the decorator pattern is more flexible compared subclassing

2, mainly to solve:

  In general, we often use in order to expand a class inheritance to achieve, due to the introduction of a static class inheritance features and extended functionality with the increase, the subclass will be expanded

3, When to use:

  In the case of extension classes do not want to increase the number of subclasses

4, how to solve:

  The specific functions of segregation of duties, while inheriting the decorator pattern

5, the key code:

  1, Component class acts as an abstract role, should not the specific implementation
  2, modified class references and inheritance Component class, specific extension classes override the parent class method

 

6, application examples:

1, the Monkey King 72 change when he becomes a "temple", he simply was a monkey, but he has a temple function
2, whether there is not a picture frame can be hung on the wall, but there are usually a picture frame, and the frame is actually hung on the wall. Prior to the wall, can be covered with painted glass, mounted to frame yard; Videos this case, a glass and a frame body formed

 

7, Advantages:

Decorative and decoration can be developed independently, not coupled to each other, the decorative pattern is an alternative mode of inheritance, decorative patterns dynamically extend the functionality of a class implemented

 

8, usage scenarios:

1, extension of a class
2, a dynamic addition, dynamic revocation

 

 

  As can be seen from FIG Circle / Rectangle are decoupled and RedShapeDecorate

Guess you like

Origin www.cnblogs.com/july-sunny/p/12635550.html