The Decorator Pattern

原创 hamo  2007-03-15 10:19  阅读 1,879 views 次

Decorator - Attach additional reponsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

CName: Wrap

OO Principles:

  • Classes should be open for extension but close for modification.

Bullet Points:

  • Inheritance is one form of extension, but not necessarily the best way to achieve flexibility in our designs.
  • In our designs we should allow behavior to be extended without the need to modify existing code.
  • Compostion and delegation can often be used to add new behaviors at runtime.
  • The decorator Pattern provides an altertive to subclassing for extending behavior.
  • The Decorator Pattern involves a set of decorator classes that used to wrap concrete components.
  • Decorator classes mirror the type of the components they decorate.(In fact, the are the same type as the components the decorate, either through inheritance or interface implementation.
  • Decorators change the behavior of their components by adding new functionality before and/or after(or even in place of) method calls to the components.
  • You can wrap a component with any number of decorators.
  • Decorators are typically transparent to the client of the components; that is, unless the client is relying on the component's concrete type.
  • Decorators can result in many small objects in our design, and overuse can be complex.
版权声明:本文为原创文章,版权归 hamo 所有,欢迎分享本文,转载请保留出处!