SlideServe
Menu
Préférences
© SlideServe · v2026

Flexible Object Functionality Extension with Decorator Pattern

tinagrant avatar
11 diapositives · 275 Views · Jan 05, 2020

Decorator pattern provides a dynamic way to attach additional responsibilities to objects without subclassing, offering flexibility for extending functionality. It allows for individual objects' customization, like adding borders or scrolling to UI components. This pattern encloses objects within decorators, enabling recursive nesting for unlimited customization possibilities.

Flexible Object Functionality Extension with Decorator Pattern - slide 1
01 / 11Flexible Object Functionality Extension with Decorator Pattern
1 / 17

Télécharger la présentation

Flexible Object Functionality Extension with Decorator Pattern
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Decorator Pattern • Intent • Attach additional responsibilities to an object dynamically. • Provide a flexible alternative to subclassing for extending functionality

  2. Decorator – Motivation • Motivation – Applicability • Need to add responsibility to individual objects not to entire classes Add properties like border, scrolling, etc to any user interface component as needed • Enclose object within a decorator object for flexibility Nest recursively for unlimited customization

  3. Decorator – Example • Compose a border decorator with a scroll decorator for text view. a_border_decorator component a_scroll_decorator component a_text_view

  4. Decorator – Example Diagram VISUAL_COMPONENT * draw * TEXT_VIEW + DECORATOR * draw component : VISUAL_ COMPONENT BORDER_DECORATOR + SCROLL_DECORATOR + draw border_width draw_border draw scroll_position scroll_to

  5. Decorator – General Structure COMPONENT * method * CONCRETE_COMPONENT + DECORATOR * component : COMPONENT method CONCRETE_DECORATOR_B + CONCRETE_DECORATOR_ A + method another_feature method other_feature

  6. Recursively do method for next in chain Decorator – Implementation • deferred class COMPONENT • feature method isdeferredend • class CONCRETE_COMP feature method is... end • deferred class DECORATOR feature component : COMPONENTend • class CONCRETE_DECORATOR featuremethodispre_actions component.method post_actionsend

  7. Decorator – Applicability • Add responsibilities to individual objects dynamically and transparently Without affecting other objects • For responsibilities that can be withdrawn • When subclass extension is impractical Sometimes a large number of independent extensions are possible Avoid combinatorial explosion Class definition may be hidden or otherwise unavailable for subclassing

  8. Decorator – Participants • Component Defines the interface for objects that can have responsibilities added to them dynamically • Concrete component Defines an object to which additional responsibilities can be attached • Decorator Maintains a reference to a component object and defines an interface that conforms to COMPONENT • Concrete decorator Add responsibilities to the component

  9. Decorator – Consequences • Benefits • More flexible than static inheritance • Can add and remove responsibilities dynamically • Can handle combinatorial explosion of possibilities • Avoids feature laden classes high up in the hierarchy • Pay as you go when adding responsibilities • Can support unforeseen features • Decorators are independent of the classes they decorate • Functionality is composed in simple pieces

  10. Decorator – Consequences – 2 • Liabilities • From object identity point of view, a decorated component is not identical • Decorator acts as a transparent enclosure • Cannot rely on object identity when using decorators • Lots of little objects • Often result in systems composed of many look alike objects • Differ in the way they are interconnected, not in class or value of variables • Can be difficult to learn and debug

  11. Decorator – Related Patterns • Adapter changes interface to an object, while Decorator changes an objects responsibilities • Decorator is a degenerate Composite (only one component) • Strategy lets you change the internals of an object, while Decorator changes the exterior