Design Patterns: decorated with patterns

Decorator application scenario covers, extended, IS-A, front, writing - Agent 

Decorator Pattern ( Decorator Pattern) refers to the base without changing the original object, with the additional functionality to an object, providing more flexible than inherited alternatives (expansion of the original object function) 

Decorator in code program applies to the following scenarios:

1, for extension of a class or a class to add additional functions.

2, the dynamic object is added to a function, these functions may be dynamically re revoked.

 

Code Example: Inheritance (longitudinal extension) and decorative (scale)

 

 

 

 

 

 

 

 

 

 

 

 

Decorated with contrast and adapter

 

 

Advantages and disadvantages of the Decorator Pattern

advantage:

1, decorator inherited a strong complement, flexible than inheritance, without changing the original object dynamically case next to an object extensions, plug and play.

2, by using different combinations and permutation of these decorative decoration may achieve different effects.

3, decorator full compliance with the principle of opening and closing.

Disadvantages:

1, there will be more code, more classes, increasing program complexity.

2, dynamic decoration, more complex multilayer decorative. 

 

Guess you like

Origin www.cnblogs.com/LPJ-BLOG/p/12444497.html