IntroductionIn object oriented design (OO), we give clear responsibility to each object. An object is like a black box which can receive and send messages. All objects together form the complete system. Design patterns by Gang of Four introduced many general reusable solutions to commonly occurring problems in software design. I always correlate interactions between objects to real life interactions involving people and or real things. If you look around, you can see many real life examples for patterns, but played out with people instead of real objects. In this article, I will try to explain a few of these real life examples of design patterns for you.
A typical example for this in the software scenario is an XML DOM (Document Object Model) structure. A simple XML structure is shown below.
As you can see here, “
employees” is a node. “
employee” is another node which is a child of
employees. What about
employee? That is also a node. It is nothing but a collection of node objects. Each node can have n-number of child-nodes. If I draw a class diagram of the same, it will be like shown below.
If I create an object structure for the same in a system, based on the XML file input, you will have a structure which is as simple as a single object to a complex structure involving any number of objects. A typical structure is shown below:
To describe the advantages of the Composite pattern, I would like to take a simple organization structure as shown below. If you want to find the total number of employees in the organization, it is the sum of the root node + all its children. The calculation goes recursively, as shown below:
Another typical example of the power of recursion is GUI toolkits. For example, Java SWT (Standard Widget Toolkit). If you want to apply a theme for a dialog, you need to apply the theme for the dialog + all its child components. A child can be a composite or a widget itself. If it is a widget, there is no child for that, and the theme needs to be applied only for the widget. But if it is a composite, we need to apply the theme for all its children. Now again, a child can be a widget or a composite. The loop continues until it covers all child widgets.
Another example I can think of is the destruction of objects in C++. Take any OO language without a garbage collector. Just imagine you have a data structure like shown below. Each object has a reference only to his children and the client is aware of only the root node object. How will you destroy all the objects, leaving no memory leak? It is only a couple of lines of code that takes to destroy the complete code.
Kill a node = Kill all its children and then kill yourself.
Since the children are also of type node, the loop continues till it goes to the lowest level where there are no children.
It is the same in a GUI system as shown below. Your business logic, in this case, the cassette player, will update all the GUI components periodically, just like a post man who comes to your house with mail. It would have been inefficient for the total system to implement a periodic check by each widget to get the progress.
There are two typical models for Observer based on the way data is passed. It can be a push model or a pull model. In the push model, data is pushed along with the notification. An example for this is mail delivery, magazine subscriptions etc. In the pull model, the client will be notified about what is happening. But, it is always the responsibility of the client to check whether the relevant data is there or not. An example for this is RSS feeds. Whenever there is a change in the website, and if you have subscribed for the feed, you will get a notification. But, it is up to you to go back and see if you are interested.
Now, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.
Another example for this is a translator who is translating languages spoken by one person for another person.
Other examples for Façade are one stop bill payment shops, a support desk in a big organization which takes all kinds of support requests etc.
A software example for Decorator is JAVA I/O. Initially, I wondered why we need to create so many objects just to do a file operation. I realized how flexible and wonderful it is after some time.