Thursday, February 23, 2012

When to use and when not to use a Design Pattern?

What is a design Pattern?
A solution to a common problem in object-oriented programming context.

Base requirement is: We should not complicate the solution, i.e. it should be simple and clean.

When to use a Design Pattern?
Scenario1: Starting solution without using any pattern and later you can decide to refactor the solution to add certain design pattern after evaluation, if it is applicable and can be correctly applied.

Scenario2: When you are pretty sure that you can correctly apply a design pattern, then you can start your solution directly with it or doing some sort of proof-of-concept before starting.

Points that should be remembered here:
1. Try NOT doing over-engineering of the solution which may complicate it and will result into maintenance nightmare.
2. Follow KISS principle, i.e. keep your solution design simple.
3. Use design pattern only when it's really needed.
4. Following any given pattern rigidly is not really the answer.
5. Using design patterns is a good coding practice, but again it doesn't mean that writing a simple and clean solution without any design pattern is bad. We just have to identify the points where in future the application have to be made scalable should not make the simple solution complex. Using design patterns can keep it simple to enhance in future.

Frankly speaking, the rule is 'there is no rule' about when to use and when not to use a Design Pattern. Your experiences (success more than failures) when to use them purely, when to adapt them or when not to use them at all will be a deciding factor here :)


Happy coding :)

No comments:


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________