According to Gof: "Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality"
Closed for Modification and Open for Extension
One of the main challenges we face in development is Change. The Closed for Modification and Open for Extension principle says a new functionality can be added by keeping the original code unchanged.
Example:
You have an Album class today which is just blank now. Tomorrow if the customer wants a Christmas tree on that.. What would be our approach?
We will modify the original Album class to incorporate the Christmas tree on it. This should not be the best approach. There is a better approach to this. We can still keep the Album class unchanged and add the Christmas tree to it. Everything happens in the runtime… that is the cool part of it.
Some more examples
We can see real life controls like Form, Button etc. There would be a Form class with built-in functionality. Still the user can use it and add new controls to it / extend the functionality. Here we will be basically deriving from the existing Form/Button class and add new methods or properties to it.
The difference between the above approach and Decorator pattern is that, in the Decorator pattern, it is done during runtime.
Conclusion on Change
So basically we can conclude that whenever changes are required, the possible solutions could be:
- Change the original class
- Subclass it and create instance of subclass
- Use Decorator Pattern and still using the original class instance
Requirement
The requirement here would be to provide a default Album object and based on dynamic requirement from the user in runtime, we have to draw other pictures to the album.

Design
Our first class would be the Album class which has a Graphics object as parameter.
It contains a Draw() method which is virtual and just clears the graphics object.
public class Album
{
public Graphics Graphics
{
get;
set;
}
public Album()
{
}
public Album(Graphics graphics)
{
Graphics = graphics;
}

Gary GuoPosted Dec 23, 2010, 12:08 PM
Dynamic Decorator extends functionality of an object without modifying its class or writing decoration code during design time. Here is the link to the article http://www.codeproject.com/KB/architecture/dynamicdecorator.aspx