Pattern
"Design Patterns are general, repeatable solutions to common recurring problems in software development."
A "pattern" has been defined as "an idea that has been useful in one practical context and will probably be useful in others".
A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.
In this article I'll try to share my thoughts of one of Structural Design Pattern named Decorator.
Structural Design Patterns are concerned with how classes and objects are combined to form larger structures.
Let's jump into this and try to swim.
Definition
The Gang Of Four (GOF) defines the Decorator pattern as "Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality."
Closed for Modification and Open for Extension
One of the main challenges we encounter 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. To explore more on this kindly visit: The Open Closed Principle of SOLID
In our applications we occasionally create an object with some basic requirements and later we'd like to add some more functionality to this object dynamically with additional subclasses, in that scenario we use the Decorator Pattern.
Let's assume we have an object that saves a stream in a Comma Separated Value (CSV) format. Later we need to add more behavior to the same object to save that stream in a bit more secure format like a PDF with a password.
Here is the class diagram of the Decorator depicted below:
Participants
The classes and/or objects participating in this pattern are:
- Component (Pizza)
- Defines the interface for objects that can have responsibilities added to them dynamically.
- ConcreteComponent (ThinCrust,ThikCrust)
- Defines an object to which additional responsibilities can be attached.
- Decorator (Decorator)
- This defines the interface for all the dynamic functionalities that can be added to the reference to a ConcreateComponent.
- ConcreteDecorator (OnionPizzaDecorator, CheezPizzaDecorator,)
- Adds responsibilities to the component.
To understand the decorator pattern let us extend the example being used here. We have a Pizza class as a base class. Later we may add more stuff, like toppings on each Pizza, for example extra cheeze, Onions and Jalapeno with Baby Corn.
I have chosen a very simple example (Pizza) because most people would be familiar with this. Though there could be the same scenario for other objects like for Ice Cream and Coffee.
Let us start by creating the component class.
public abstract class Pizza
{
public abstract double GetPrice();
}
Here is the ConcreateConponent class defines the functionality of the base class Pizza.
class ThikCrust : Pizza
{
private double p_Price = 250.0;
public override double GetPrice()
{
return p_Price;
}
}




Biswajit PandaPosted Nov 19, 2013, 7:48 AM
Excellent Article