The Decorator Design Pattern using VS 2010

The decorator pattern can be used to extend the functionality of a certain object at runtime, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class.

Decorator in abstract –

image

Source: Wikipedia.org

Suppose, you are commissioned for the software program which produces Computer, along with peripherals, sales. Assuming, you design a Computer class with description method which returns some string value identifying it’s peripherals support viz. monitor, hard-disk, pointer device, keyboard, etc.

Without knowing much of the vision ahead, you design a class like this –

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer!"); }
     }
}

and when company decided to sale the Computers with assembled peripherals like monitor –

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer with a monitor!"); }
     }
}

along with, some input and pointing device like mouse, keyboard and storage such as hard disk.

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer with a monitor, pointing device, keyboard and a harddisk!"); }
     }
}

Well, this is one way of avoiding kind of situation to modify the class perpetually add the code and workaround for the solution. Let us look on Decorator design pattern how it will help us to solve this problem by writing a wrapper class where developer no longer have to modify the method for updates.

Starting off again, we have Computer class with Description method. Instead of modifying it every time and to implement Decorator pattern, lets create a abstract class with extending Computer class with Description method and pass on the Computer object to the Disk class. Thus, Disk object will call the Computer.Description() method in its own Description method and will add the text return from Computer.Description(). Lets look at the steps –

Wikipedia says –

  1. Subclass the original “Decorator” class into a “Component” class (see UML diagram);
  2. In the Decorator class, add a Component pointer as a field;
  3. Pass a Component to the Decorator constructor to initialize the Component pointer;
  4. In the Decorator class, redirect all “Component” methods to the “Component” pointer; and
  5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

In general,

  • Create the  abstract decorator class extending Computer object.
  • Create a class extending decorator abstract class.
  • Take the object which has to be extended and store object.
  • Call base method with will return value and extend/manipulate the value and return back.

Lets see how the decorator can be implemented using VS 2010

Computer component:

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public string Description() { return "This is computer with motherboard, ram, 4 usb connectors"; }
     }
}

 

Decorator –

namespace DecoratorPattern.Abstracts
{
     public abstract class ComponentDecorator: Computer
     {
         public abstract string Description();
     }
}

Concrete decorator –

namespace DecoratorPattern
{
     public class Disk: ComponentDecorator     
{
        Computer _computer;
        public Disk(Computer c)
         {
             this._computer = c; ;
         }
         public override string Description()
         {
             return _computer.Description() +  ", disk";
         }
     }
}

Download source code here

References – Wikipedia.org, VTC examples.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: