How Design Patterns Solve Your Problems
Design patterns exist to capture best practices and propagate solutions to commonly recurring problems. The solution lies in establishing the structure and responsibilities of the individual classes and objects that make up the pattern. The main point, however, is that patterns help to identify and isolate system-level variability so the software can evolve over time without adversely affecting the underlying design structure.
The fundamental goals of creating good object-oriented designs are:
- Encapsulate system variability
- Favor composition over inheritance
- Design an interface
How to Select the Right Design Pattern
When a developer decides to use a design pattern to overcome an existing issue, he or she has to choose the kind of design pattern that will fulfill this requirement. The first step is to identify which type of design pattern -- creational, structural or behavioral -- is going to work well for the problem?
- If you are trying to separate the process of object construction then use creational design patterns such as the singleton, factory, abstract factory and builder patterns.
- If you are trying to interface to a sub-system, connect to an interface your system doesn't support, or provide a flexible storage structure for adding and manipulating objects, then use structural design patterns such as the bridge, composite and façade patterns.
- If you are trying to access objects, perform operations or interact with operations, use behavioral patterns such as the command, iterator, observer, strategy and visitor patterns.
Now that you have determined which category your problem exists in and you have identified the principle problem you are trying to address, you can begin searching for a pattern that is designed to match your intention.
If you have articulated your intention correctly and cannot find a pattern to match, don't fret. A design pattern may not be your solution. Design patterns address problems that occur over and over again so that the solutions they offer can be replicated. Your problem may not occur over repeatedly and thus does not have a repeatable solution.
In three upcoming articles, I will go through each design pattern category in detail, including definition, benefits, usability and examples.