Decorator pattern provide ways of attaching new state and behavior to an object dynamically.
1. The object being decorated is untouched so it does not know it is being decorated.
2. There is no one big feature-laden class with all the options in it.
3. Decorations are independent of each other.
4. It does not rely on inheritance for extending behavior. Instead it instantiate the object and adds new decorations in it.
5. Decorator pattern does not need any advanced language feature; it rely on object aggregation and interface implementation.
6. Decorators can inherit directly from the component and maintain an object of that class as well.
Use of Decorator pattern-
1. Any Input/ Outbound bound process- like video streaming can be compressed at different rates and sound can be input to a simultaneous translation service. Consider the following hierarchy:
System.IO.Stream System.IO.BufferedStream System.IO.FileStream System.IO.MemoryStream System.Net.Sockets.NetworkStream System.Security.Cryptography.CryptoStream
The subclasses decorate Stream because they inherit from it, and they also contain an instance of a stream that is set up when an object is constructed. Many of their properties and methods relate to this instance.
2. Consider desktop browser and a mobile browser, mobile browser creates display object suitable for smaller screen including scroll bars and excluding any banner.
3. Decorator pattern so do useful that they are in .NET 3.0. System.Windows.Controls provides a base class for creation of custom controls.
Use Decorator Pattern when you have-
An Existing component class that may be unavailable for sub-classing.
Use Decorator pattern when you want to-
1. Attach additional state or behavior to an existing object.
2. Make changes to some objects in a class without affecting others.
3. Avoid sub-classing because too many classes could be the result.