Monday, December 13, 2010

Design Patterns - An eye for similarities - Part 3

Decorator Pattern - Wrap me, my brother


The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality.
Typically, the decorator pattern essentially adds a wrapper around a class to add additional capability to it.


This pattern works on the principle of "Open for extension, closed for modification".
So, when we need to add additional capability to the existing object we don't modify the parent class to do that, but make a new class and wrap the existing object in this new class that provides the necessary capability.
For eg. we have a class Mammal and we want to add functionalities to create a Human. We just create a new class HumanDecorator that takes in the Mammal as an object and adds the necessary intelligence into it, thus eliminating the need to change Mammal as a whole. The thing to note here is that both the Mammal and the HumanDecorator class should have the same type. This is done to maintain the extensibility.

Part 4 - Factory Pattern

Sunday, December 12, 2010

Design Patterns - An eye for similarities - Part 2

The Strategy Pattern - Divide and Rule

The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.[Taken from Wikipedia]

In essence, all the strategy pattern says is to identify the things in the application that can change (verbs) in the application and separate it from the things that don't (nouns).
Generalize the verbs into a family of interfaces and create encapsulations for each of them which form the concrete classes.
E.g: [ From the book - Head First Design Patterns]

The Observer Pattern - Tell me, I'll let you know

The observer pattern defines a one to many dependency between objects so that when one object changes state, all of it's dependents are notified and updated automatically.

The Observer Pattern consists of a publisher that publishes events and subscribers that subscribe to that event. Thus, in effect, it helps makes the system loosely coupled by providing an interface for 2 objects to talk without actually having to maintain any relationship.
For example in a shopping application the stock manager and the sales manager would have subscribed to the event on the click of the "BUY" button on an inventory. Thus the cart might fire an event saying that a particular inventory has been bought. The sales and the stock manager having subscribed to the event get notifications to process the information furthur.

Part 3 [Decorator Pattern]
Part 4 [ Factory Pattern ]

Design Patterns - An eye for similarities - Part 1

Almost in every software that we built there are a lot of algorithms and rules that tend to beat around the same bush. Obviously a lot of people before us have found that out and have wondered on how some generic solutions could built for them so as to make life a bit simpler. The result of this was Design Patterns. In this multi-part series we'd be look at them patterns, how they help in finding bottlenecks and how to get around them.

So how do we find on where to use patterns?

This for most people is the toughest part. And true sometimes it is really confusing. But with careful thought and proper application I think we can definitely get to a point where we recognize them with ease.
- First and the foremost read through the requirements. Very important.
- Ask questions and lots of them. The more clear you are on the requirements, the better you can visualize the system.
- Find out all the "is-a", "as-a" and "has-a" relationships. This helps you in building up the hierarchy, interfaces and abstractions.
- Have a thorough knowledge of the existing design patterns and go through loads of examples to understand the nuances with each.
- Also a familiarity with anti design patterns does not hurt either.

Hope this gives a basic idea of what to do when getting ready to design applications. :)
Happy Designing.. :)

Part 2 [Strategy and Observer Patterns]
Part 3 [ Decorator Pattern ]
Part 4 [ Factory Pattern ]