最新消息:

The Decorator Pattern

读书学习 hamo 1078浏览 0评论

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.

转载请注明:哈默博客 » The Decorator Pattern

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址