Design Patterns Series 3 - Decorator Pattern

In previous post we explored Strategy Pattern, today we will explore another design pattern, that take care of flows in standard OOP especially  when it comes to inheritance.

Decorator pattern :
The decorator pattern is perfect for the opening scenario, it's all about extending the functionality of given class. After you have written a class you can add decorators(additional classes) to extend that class, that mean you will not have to keep modifying the original class's code over and over again.

It allows you to write your code and avoid modification, while still extending that code if needed. So as much as possible design your core code so that  it doesn't have to be modified a lot, but may extended  as needed.

If you want to write a code to produce a car with description as "this is a new car", so the class car will be like the following
public class Car
{
   public Car()
   {
   }
   public String Description()
   {
      return "that is a new car";
   }
}
that is so simple but what about cars with air condition. you have to modify car description to "this is a new car with air condition"
public class Car
{
   public Car()
   {
   }
   public String Description()
   {
      return "that is a new car with air condition";
   }
}
again what about a car with air condition with central lock??. modify car class again
public class Car
{
   public Car()
   {
   }
   public String Description()
   {
      return "that is a new car with air condition with central lock";
   }
}
you need to add new feature to your car so modify your code again and again, so what should we do? remember close your code for modification but open it for extension. you are right decorator pattern will do that.
A better name for this pattern might be the "Augmentor" or "Extender" pattern;
because that's what it allows you to do: augment or extend a class; dynamically at runtime.
when you use wrapper code to extend your core functionality and you don't need to modify it, you are decorating the code.
To use decorating pattern in your code firstly you should close your core code to modify, we can assume the first car class is the core of our code
public class Car
{
   public Car()
   {
   }
   public String Description()
   {
      return "that is a new car";
   }
}
now we need to open our code to extension, we can do that by make all car class wrapper  have to inherit Car class
Now we can create a car with air condition using wrapper, AirCondition, which add air condition to the core car and because it is a wrapper class it has to know what it is wrapping so you can pass a car object to its constructor.
public class AirCondition : Car
{
   Car car;
   public AirCondition(Car _car)
   {
      car = _car;
   }
   public override String Description()
   {
      return car.Description() + " with air condition";
   }
}
you also can add central lock to your car using CentralLock wrapper
public class CentralLock: Car
{
   Car car;
   public CentralLock(Car _car)
   {
      car = _car;
   }
   public override String Description()
   {
      return car.Description() + " with central lock";
   }
}
as we see from examples we wrote our core functionality and did not modify it, just we extended it, the following code fragment will show us how to use wrappers class to extend our core functionality in run time.
Car myCar = new Car();
myCar = new AirCondition(myCar);
myCar = new CentralLock(myCar);
myCar = new CentralLock(myCar);

Console.Write(myCar.Description());
this code fragment will print out the following description
that is a new car with air condition with central lock with central lock
Now you were able to extend the core object simply by wrapping it in various decorator wrappers, avoiding modification of the core code.That’s how you use the Decorator design pattern.

you can download the full source code here

Comments

Popular posts from this blog

ASP.Net MVC : ActionLink with bootstrap icon

ASP.Net MVC : Conditional Validation using ValidationAttribute

Android : How to change progress bar color at runtime programmatically?