Design Patterns for Dummies. The Decorator Pattern

Posted in Design Patterns
This post has been read 4056 times

Yes there are a lot of posting on design patterns, and you might ask why another of these post on design patterns. I am doing this post so that I can document the design patterns for myself for later use.

There are 23 design patterns as observed by the Gang Of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides). And they are categorized basically into 3 segments viz. Structural, Creational and Behavioral patterns. I will take these in order.

To begin with I will start with the Decorator pattern.

Decorator pattern comes under Structural patterns which dwells on how classes and objects are designed to form a much larger structure. Usually these patterns play a major role in the post production or the maintenance stages of the development cycle, when you need to add some additional functionality without changing the existing classes. The decorator comes into play when you need this, i.e. when you want to add functionality to a class without changing it. Like the decorator, the proxy and the bridge pattern also helps in adding dynamic functionality which I will discuss in a later post.

OK. Lets see some code of the decorator pattern.

    1 public class Person

    2 {

    3     public string Name { get; set; }

    4     public int Age { get; set; }


    6     public Person()

    7     {

    8         this.Name = "Person1";

    9         this.Age = 27;

   10     }


   12     public virtual void GetDetails()

   13     {

   14         Console.Write("My name is {0} ", Name);

   15         Console.Write("and my age is {0}\n", Age);

   16     }

   17 }


   19 public class Decorator

   20 {

   21     public class PersonLocation : Person

   22     {

   23         Person _person;

   24         string _location;


   26         public PersonLocation(Person person, string location)

   27         {

   28             _person = person;

   29             _person.Name = "New Person1";

   30             _person.Age = 28;

   31             _location = location;

   32         }


   34         public override void GetDetails()

   35         {

   36             base.GetDetails();

   37             Console.WriteLine("and i live in {0}", _location);

   38         }

   39     }


   41     public class PersonJob : Person

   42     {

   43         Person _person;

   44         string _jobDesc;


   46         public PersonJob(Person person, string jobDesc)

   47         {

   48             _person = person;

   49             _person.Name = "New Person2";

   50             _person.Age = 30;

   51             _jobDesc = jobDesc;

   52         }


   54         public override void GetDetails()

   55         {

   56             base.GetDetails();

   57             Console.WriteLine("and i work as a {0}", _jobDesc);

   58         }

   59     }

   60 }


   62 public class Program

   63 {

   64     public static void Main()

   65     {

   66         Person p = new Person();

   67         Decorator.PersonLocation pLocation = new Decorator.PersonLocation(p, "Miami");

   68         pLocation.GetDetails();

   69         //Output : My name is Person1 and my age is 27

   70         //         and i live in Miami

   71         p = new Person();

   72         Decorator.PersonJob pJob = new Decorator.PersonJob(p, "Developer");

   73         pJob.GetDetails();

   74         //Output : My name is Person1 and my age is 27

   75         //         and i work as a developer

   76         Console.ReadLine();

   77     }

   78 }

Here the classes PersonJob and PersonLocation are decorators around the Person class. Person class has only 2 properties viz. name and age. We are dynamically adding 2 new properties which are job and location through 2 decorators.

One thing to note here as you see is that even though we are changing the person’s properties in the decorator, in the output the name and age doesn't change. This is because the decorator pattern is based around new objects being created with their own sets of operations.

When to use the decorator pattern : We can use the decorator pattern when we want to, as discussed earlier, change some objects in a class without affecting the original class, when we want some behavior for an object dynamically.

Comment posted on Wednesday, March 18, 2009 10:33 PM
Hmm, I admit I'm not a patterns expert, but I don't think this implementation is correct.  The properties on the decorator that it inherits from the base should be routed to the decorated object, ie PersonJob.Name should return _person.Name.  Typically, I think you use an interface (so IPerson here) and have the decorator implement that interface instead of deriving from the concrete type.  
Anti Bot Image:

Insert Cancel


Random Photo

My Tweets

Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.


Recent Blogs