※ design pattern ※ → ☆ ☆ ============ Decorator structural model Mode (IX)

Overview mode

In the design of software components, if the division of responsibilities is not clear, the results are often obtained by using inheritance as demand changes, rapid expansion of sub-categories, while full of duplicate code, this time is the key to draw responsibility. A typical model represents: Decorator , Bridge

Core Bridge is to achieve separation and abstraction, so that when you need to change the two separate classes of behavior and attributes from him, Bridge mode can be used to achieve properties in abstraction, to achieve behavior in the realization of the body, so as to achieve the properties and behavior can independent changes, via a bridge (that is, binding them, hidden in an abstract class) between their way to be associated, and the relationship of this bridge is bound to be changed dynamically, which is compared with the maximum inheritance advantage, sometimes we can also interpret it as an evolution of the agency model, the agent behavior to the behavior of the various implementation classes to deal with.

※ design patterns ※ → ☆ ☆ ============ Bridge structural model mode (seven)

Above Bridge mode, type of computer (abstract) and computer manufacturers (entity) via a bridge (i.e. bind to both hidden in an abstract class) in the manner to achieve the properties and behavior can vary independently.

For example, a classic example of the coffee: a cup of coffee, for example, there are cups and mugs in the points, as well as milk without the milk extra points if a simple inheritance, these four specific implementation (cup mug milk. there are no milk) between concepts overlap because there are cup milk, but also in the cup without milk, if this level again in the cup and then implement two inherited, it is clear that confusion, poor scalability. then we Bridge mode used to implement it. cup / mug (abstract) and the milk / without milk (solid).

        Decorator pattern applied to the subclass inheritance hierarchy is too deep, the inheritance can be very clever to reduce the number of layers, and effective in reducing the amount of code required to implement the function, no doubt is a weapon OO thinking. When you need to add new operations to a class, we used to practice such as the parent class, then inherit a subclass, the implementation will need to put subclass. However, this will bring a problem when we need to function as the project progresses gradually increased, we have several items for each additional operation, you inherit a subclass, to a certain extent when we find that the number of layers will inherit depth would be unprecedented, and the abstract base class to achieve polymorphism, a need to add a pure virtual function for each operation. Similar problems, Decorator pattern gives a good solution, in an abstract base class, we will see how virtual functions less.

 

Decorator pattern to an object dynamically add some additional responsibilities. Increases functionally, Decorator pattern compared subclassing more flexible. Under the premise of not changing the interface, enhance the performance of the considered class.

  •     1) need to extend the functionality of a class, or a class to add additional responsibilities.
  •     2) to dynamically add functionality to an object, these functions may be dynamically re revoked.
  •     3) the need to increase the number of permutations and combinations of the basic functions generated very large number of features, so that it impractical inheritance.

 

Decorator pattern composed of the following roles

  •     Abstract component (Component) Role: given an abstract interface to regulate ready to receive additional responsibilities.
  •     DETAILED member (ConcreteComponent) Roles: define a responsibility to be received additional class
  •     Decorative role (Decorator): a member holding the object (Component) Examples and define a consistent interface to the interface with the abstract component
  •     Specific decorative role (ConcreteDecorator): responsible for component object "labeled" additional responsibility
     

 

Mode structure

Mode discussion

Decorator pattern used in some cases we may be "excessive use inheritance to extend object function", as the foundation for the introduction of static type especially, makes this extended mode inflexible; and with the increase of the subclass ( extensions increase), the combination (the combination of extensions) of each subclass can cause swelling of the various subclasses.

  • In some cases we may be "excessive use inheritance to extend object function", due to the static nature of inheritance for the type introduced, making this expansion mode inflexible; and with the increase (extension of the increase subclass ), the combination (the combination of extensions) of various subclasses results in more expansion subclass. That we need to find a way to achieve the functionality is extended by non-inheritance, but the interface does not change classes take place.
  • How to make "extension object functions" can be achieved dynamically as needed? While avoiding the "extended functionality to increase," sub-class expansion problems caused? So that any "functional
    expansion and change" caused by the impact will be minimum.

According to the above, we can see the Decorator pattern has the following main points

  • 1. By using a combination of techniques are not inherited, the Decorator pattern to achieve the ability to dynamically extend functions of the object at runtime, and may need to be extended in accordance with a plurality of functions. Avoid the use of inheritance caused by "inflexible" and "multi subclass ramifications."
  • 2.Decorator performance of the interface class inheritance relationship is-a Component, i.e. Decorator class inherits the Component class has an interface. But in relation implemented as a combination of performance and has-a Component, i.e. Decorator turn uses another Component class.
  • 3.Decorator object model does not solve the "multi-word derived class multiple inheritance" problems, the gist of Decorator mode application is to solve the "principal class extension in multiple directions" (obviously file, network and encryption, are two buffer extended direction) - is "decorative" means.

Decorator and Bridge of difference

Decorator is an enhancement to existing functionality, they are decorative and functional decoration revolves around the existing class is, and no matter how decorative change, it must be a change around the decorated class (or to around two common interface change), because for the client, calling a decorative object before and after should be the same.

Bridge mode is separated from the behavior of the object to be bridged (abstract) and out into the full implementation class to implement the behavior achieve less restricted bridged object, so if this constraint is necessary, can using Decorator mode, Bridge mode and vice versa may be employed

 

Difference Decorator, Bridge, Adapter's

  • Adapter : the two incompatible classes gathered together using the (post-project development compatible)
  • Bridge : abstract and behavioral division open, independent, but can dynamically binding (project development early in the design)
  • Decorator : dynamic to an object to add some additional responsibilities (project development later maintenance)

In order to enhance understanding, I still use a computer [members] (desktops, notebooks, servers), manufacturers [role] (ASUS, DELL, HP) to handle the case. Whether it is a computer type of change, or changes in vendors. Once the change with the needs of a subclass of rapid expansion, while full of duplicate code, the key is to draw the responsibility.

Bridge : By combining the type of computer (abstract) and computer manufacturers (entity) mode, an abstract class 3 * 3 entity class may be expressed nine types of combinations of computers. But you can not computer manufacturers (entities) are mixed, for example: a computer function both ASUS, HP another function.

The Decorator : add functions by computer type (member) and computer manufacturers (character) manner, the role of class 3 + XX member may be expressed in combination various types of computers. + ASUS desktop function, function desktop + ASUS + DELL functions. Compared Bridge more flexible terms.

Can be seen from the UML diagram, if the right is limited to achieve the object left bound necessary, can be employed Decorator mode, Bridge mode and vice versa may be employed

 

Finally, no matter how privileged mode, but we also need to carefully consider the actual situation.

 

Pattern implementation

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
after Xiao Ting notes will be as detailed as possible explain some knowledge, I hope you continue to focus on my blog.
This section notes to the end here.

Xiao Ting will have time to put their learning experience, feel better knowledge to write and share with everyone.
Programming development of the road is very long, very much like to be able to share together, learn together, and common progress.
If you have any articles oversight place, please correct me. I hope you and I can be more comments to discuss issues related to programming.
Finally, thank you for your support ~~~ been

       Complete C ++ code example (the code under test can be run in VS2017)
codes and related information Download:
              https://gitee.com/arvinxt/DesignPattern

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103569917