Ive been saying to myself that I will set aside time, to write a tutorial on design patterns. I think that time has come and here is what I have come up with.
What are design patterns?
In the object oriented world the solutions/designs we create to overcome problems or requirements have probably been done before. Over time these solutions/designs have evolved into repeatable set of patterns. Some of these patterns have been identified and classified into one of the three types of design patterns.
However there is no hard and fast rules for these patterns and you might have already been using this patterns unconsciously. However there are a few patterns that are well documented and widely used. Even these patterns might have several variants and be customiz3d to solve specific problems.
What are the types of design patterns? How man are there in total?
There are three main types of design patterns. There are several design patterns and their variants in use, here are a few that have been well documented and frequently used.
Creational Patterns
Abstract Factory
Builder
Factory Method
Prototype
Singleton
Structural Patterns
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Behavioral Patterns
Chain of Resp.
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
Why do we need design patterns? Are they really useful?
Design patterns help implement the object oriented concepts. They also provide us with a standard set of available and well researched solutions to familiar software design problems.
"Are they really useful" is a hard question to answer and also quite opinionated, but My understanding is Yes they are useful. My justification for this is, by implementing design patterns in the long run you will create code that is standardized and understandable by all. Further there is no point in re inventing the wheel, especially with less efficient code and designs.
How do I know when to apply which pattern?
Unfortunately like all things abstract, this is the hardest to master. We can only learn when to apply a certain design pattern from experience and prior learning. As mentioned before there is no limitations to the number of patterns. There are a number of variants of any given pattern and every solution would require some modifications to even a well defined pattern.
However one method I have used to learn "When to use a pattern" is by looking at code written by others. Try and identify what patterns they have used to solve a particular design problem, this is your best bet in learning when to use a particular design pattern.