10. decorator pattern

  • First, the role
    • 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 (the Decorator) Role: a holding member Example (Component) object, and an interface with the abstract definition of a consistent interface member
    • Responsibility is responsible for component object "labeled" added: specific decoration (ConcreteDecorator) role
  • Second, the application scene
    • Without affecting other objects, dynamic, transparent way to add responsibilities to individual objects
    • Need to dynamically add functionality to an object, these functions may be dynamically revoked
    • When not using the system inherited way to expand or to use when inheritance is not conducive to system expansion and maintenance. Inheritance can not be employed where two main types: the first is the presence of a large number of separate extension system, to support every combination will produce a large number of sub-categories, so that the explosive growth in the number of sub-categories; the second class is defined as can not inherit (such as final category)
  • Third, examples
    • FIG -1 implementation is an example of a person's clothes pass, People member abstract character, is a common abstract classes and interfaces decorated decoration class, Jane is the concrete member (the decorator), the Decorator abstract role is decorator by polymerizing People rely on abstract components to achieve extended Jane decoration, DecoratorShirt, DecoratorSuit, DecoratorTShirt, DecoratorPants, DecoratorShoes is particularly decorative role, the role is a concrete implementation of the abstract decorator
    • Code Address: https://github.com/lanhei/DesignPattern/tree/master/src/com/lanhei/patterns/decorator

      figure 1

Guess you like

Origin www.cnblogs.com/zhongj/p/11302206.html