Decorator Design Pattern


 definition
 UML diagram
 participants
 sample code in C#


definition

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

Frequency of use:  nbsp;medium

UML class diagram

participants

    The classes and/or objects participating in this pattern are:

  • Component   (LibraryItem)
    • defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent   (Book, Video)
    • defines an object to which additional responsibilities can be attached.
  • Decorator   (Decorator)
    • maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • ConcreteDecorator   (Borrowable)
    • adds responsibilities to the component.

sample code in C#

This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object.

Show code

// Decorator pattern -- Structural example




This real-world code demonstrates the Decorator pattern in which 'borrowable' functionality is added to existing library items (books and videos).

Show code

// Decorator pattern -- Real World example




This .NET optimized code demonstrates the same real-world situation as above but uses modern, built-in .NET features.

Show code

// Decorator pattern -- .NET optimized




  • Better Code
  • Better Career
  • Better Lifestyle

Design Pattern
Framework 2.0
TM


In C# and VB.NET



Click here for details

-- Instant Access --
Instant Download