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:   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, such as, generics, reflection, object initializers, automatic properties, etc.

Show code

// Decorator pattern -- .NET optimized




   Better Code 
   Better Career 
   Better Lifestyle 


.NET
Design Pattern
Framework 4.0


 
C# and VB.NET


   Includes:

  Gang of Four Patterns
  Head First Patterns
  Enterprise Patterns
  SOA Patterns

  WPF Best Practices
  WCF Best Practices
  LINQ Best Practices

  Model View Controller
  Model View Presenter
  Model View ViewModel

  Much more...

Click here for details

-- Instant Access --
Instant Download