Yes there are a lot of posting on design patterns, and you might ask why another of these post on design patterns. I am doing this post so that I can document the design patterns for myself for later use.
There are 23 design patterns as observed by the Gang Of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides). And they are categorized basically into 3 segments viz. Structural, Creational and Behavioral patterns. I will take these in order. To begin with I will start with the Decorator pattern.
Decorator pattern comes under Structural patterns which dwells on how classes and objects are designed to form a much larger structure. Usually these patterns play a major role in the post production or the maintenance stages of the development cycle, when you need to add some additional functionality without changing the existing classes. The decorator comes into play when you need this, i.e. when you want to add functionality to a class without changing it. Like the decorator, the proxy and the bridge pattern also helps in adding dynamic functionality which I will discuss in a later post.
OK. Lets see some code of the decorator pattern.
Here the classes PersonJob and PersonLocation are decorators around the Person class. Person class has only 2 properties viz. name and age. We are dynamically adding 2 new properties which are job and location through 2 decorators.
One thing to note here as you see is that even though we are changing the person’s properties in the decorator, in the output the name and age doesn’t change. This is because the decorator pattern is based around new objects being created with their own sets of operations.
When to use the decorator pattern : We can use the decorator pattern when we want to, as discussed earlier, change some objects in a class without affecting the original class, when we want some behavior for an object dynamically.