Decorator Pattern
Posted by miteshisheth on September 11, 2009
Role
The role of the Decorator pattern is to provide a way of attaching new state and behavior to an object dynamically.
The object does not know it is being “decorated”
How to Implement
There is one interface IComponent and it is implemented to both Component and Decorator class.
Component class has original object and Decorator class has original object’s reference (type of IComponent).
Decorator class has addition property addedState and method AddedBehaviour to decorate the original object.
ICompnent interface has Operation method so both classes have same method. Method implementation are different.
So If user can call Components’s Operation method or Decorator’s operation methods.
This way we have original object is there and user want they can use Decorator’s object too.
Use the Decorator pattern when…
You have:
• An existing component class that may be unavailable for sub classing.
You want to:
• Attach additional state or behavior to an object dynamically.
• Make changes to some objects in a class without affecting others.
• Avoid subclassing because too many classes could result.
Use in :
We have to send some report to Client for sales. Sales data is is original object and we add Advertizing (at the end of report), that the Decorator
Example
using System;
// Decorator Pattern
// Shows two decorators and the output of various
// combinations of the decorators on the basic componentinterface IComponent
{
string Operation( );
}class Component : IComponent
{
public string Operation ( ) {
return "I am walking ";
}class DecoratorA : IComponent
{
IComponent component;public DecoratorA (IComponent c)
{
component = c;
}public string Operation( )
{
string s = component.Operation( );
s += "and listening to Classic FM ";
return s;
}
}class DecoratorB : IComponent
{
IComponent component;
public string addedState = "past the Coffee Shop ";public DecoratorB (IComponent c)
{
component = c;
}public string Operation ( )
{
string s = component.Operation ( );
s += "to school ";
return s;
}public string AddedBehavior( )
{
return "and I bought a cappuccino ";
}
}class Client
{
static void Display(string s, IComponent c)
{
Console.WriteLine(s+ c.Operation( ));
}static void Main( )
{
Console.WriteLine("Decorator Pattern\n");IComponent component = new Component( );
Display("1. Basic component: ", component);
Display("2. A-decorated : ", new DecoratorA(component));
Display("3. B-decorated : ", new DecoratorB(component));
Display("4. B-A-decorated : ", new DecoratorB(
new DecoratorA(component)));// Explicit DecoratorB
DecoratorB b = new DecoratorB(new Component( ));
Display("5. A-B-decorated : ", new DecoratorA(b));
// Invoking its added state and added behavior
Console.WriteLine("\t\t\t"+b.addedState + b.AddedBehavior( ));
}
}
}/* Output
Decorator Pattern1. Basic component: I am walking
2. A-decorated : I am walking and listening to Classic FM
3. B-decorated : I am walking to school
4. B-A-decorated : I am walking and listening to Classic FM to school
5. A-B-decorated : I am walking to school and listening to Classic FM
past the Coffee Shop and I bought a cappuccino
*/
Nish said
Nicely explained. I think this articles explains it in detail, so people dont have to look at any other article to understand. Looking forward to more design patterns.
miteshisheth said
Thanks Nish.
I’ve posted other articles for Prototype Design Pattern as well as for UML fundamentals.
Hope you read and give me comments for same.
Nalina said
The concepts are comprehended so well !! I found it easy to grasp the concepts .. !! Good attempt! thanks Mitesh .. keep it up .. Looking forward for more design pattens.