1 / 11

Decorator Pattern

Decorator Pattern. Richard Gesick. Pattern Overview.

beaue
Télécharger la présentation

Decorator Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decorator Pattern Richard Gesick

  2. Pattern Overview • The Decorator pattern provides a flexible alternative to subclassing by attaching additional responsibilities to an object dynamically, by using an instance of a subclass of the original class that delegates operations to the original object • This pattern type falls into the Structural Patterns category and deals with objects.

  3. Structure -- UML Construction • The ConcreteComponent is the object that can be decorated • An instance variable is included in the ConcreteDecorator for the object that it will decorate.

  4. As seen in the diagram each component can be used on its own or wrapped by a decorator.  • Decorators extend the state of the component.  • Additionally they implement the same interface or abstract class as the component they are going to decorate

  5. Attach additional responsibilities to an object dynamically. • Decorators provide a flexible alternative to subclassing for extended functionality.

  6. Applicability • Need to add responsibilities to objects dynamically and transparently • Need to withdraw responsibilities • Need to support large combinations of responsibilities without a class explosion • When extension by subclassing is impractical or impossible

  7. Consequences • More flexible than static inheritance • Avoids feature-laden classes high up in the hierarchy • A decorator and its component aren't identical • Decorators can be an effective, less-confusing and more easily maintainable substitute for multiple-inheritance.

  8. Lots of little objects! • Functionality of a class can be extended knowing only its interface; source is not always required.  Therefore, the class is closed to modification, but open to extension. • Decorators allow more flexibility to code by avoiding extending your class hierarchy just to support a characteristic. • Resources are used as they are needed since run-time objects are created.

  9. The Open-Closed Principle • Design Principle: Classes should be open for extension, but closed for modification. • Our goal is to allow classes to be easily extended to incorporate new behavior without modifying existing code.

  10. This gives us designs that are resilient to change and flexible enough to take on new functionality to meet changing requirements. • Be careful when choosing the areas of code that need to be extended; applying the Open-Closed Principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code.

More Related