What is it?
In simple words decorator enables to attach additional responsibilities(Properties) or behaviours (Methods) to an object dynamically without actually having to subclass or inherit it.
Whats the point?
The first question which comes to mind is why not inherit or subclass the object in the first place, why use the decorator pattern? Well, some of the explaination to this questions are:
- That class that is being "decorated", doesn't allow subclassing.
- You want to add state or behaviour to the object dynamically.
- You want to make changes to some object in a class without affecting others.
- You want to avoid inheriting the object as that results in too many classes and inheritance hierarchies.
Class Diagram
The following diagram shows structure of typical decorator pattern, the diagram was taken dofactory.com

The center of the above UML diagram is the Decorator class. It inherits from Component interface and also has a instance of Concrete object. This Decorator decorates the ConcreteComponent class. In the following section I've explained the pattern with an example.
Example
I've looked many examples on the various blogs and books, most of examples that I found were related to enhancing or adding functionality to an Image. For e.g. adding border, tag, watermark etc as an added behaviour to an existing Image component. These are great examples and does describe the pattern very well; however, I wanted to share an example which I've used in my day to day work. I am working in telcoms domain for last 10 years and I've used decorator pattern a few times, one of the examples is as below.
Scenario
So the scenario was that we had a third party object packaged in a .NET DLL, this Object was responsible to send SMS(Text) messages. It did work very well. Some months down the line inevitably there was a new business requirement i.e. we should also be able to send a MMS(Picture) message with the SMS. The third party object could not be inherited. I had two options to add this fucntionality
- Write a seperate component, which would only be used to send Picture Messages.
- Decorate the third party component and add picture messaging facility as an added behaviour.
The second option was the logical way forward.
The third party class library had following interface and a class
1. ISMS Interface
namespace ThirdPartyLibrary
{
public interface ISMS
{
void SendSMS(string smsText);
}
}
2. SMS Concrete Object
namespace ThirdPartyLibrary
{
public sealed class SMS : ISMS
{
public void SendSMS(string smsText)
{
/* Business logic to send SMS text message */
}
}
}
The requirement was to add additional behaviour in the above SMS concrete object, the additional behaviour would send picture message along with the text message.
The class below is the decorator pattern class which implements the additional functionality
public class SMSMMS : ThirdPartyLibrary.ISMS
{
ISMS sms;
string pictureMessageLocation = "D:/SomePicture.jpg";
public SMSMMS(ISMS _sms)
{
sms = _sms;
}
public void SendSMS(string smsText)
{
sms.SendSMS(smsText);
SendPictureMessage();
}
void SendPictureMessage()
{
/* Picture Message sending logic */
}
}
}
So as you'll notice the example starts with third party ISMS interface and a simple SMS component class that implements it. The above decorator class also implements the ISMS interface, and it includes a declaration of an ISMS, which is the object that it is decorating. Decorators always needs to know about the object that it is decorating, and in the example I've used the constructor injection. Notice in the constructor that I am passing ISMS component, which is then assigned to the sms variable.
It then implements the SendSMS function, which calls the decorated object's SendSMS method and then also calls the SendPictureMessage method, which is the added behaviour.
Now how should I use this decorator, see below an example client code:
class Client
{
ISMS smsmmsObj = new SMSMMS(new SMS())
public void sendMessage()
{
smsmmsObj.SendSMS("Test Text Message");
}
}
If you notice the client just created an object of type ISMS and called the same method "SendSMS"; however, this time the object not only sends SMS but also sends a picture message with it. The client didn't care or even know that it was the decorated object.
I hope this post helped you understand the simple example of Decorator pattern.