|
 |
A Tutorial Of Software Design: Design Patterns, Part 2
By Joe Purcell
Expert Author
Article Date: 2011-09-29
Previously, we divided software design into two aspects: design patterns and architecture patterns. Here we will look at the three kinds of design patterns: creational, structural, and behavioral.
Preface
Such an introduction would seem rather boring to those who don't see the value of design patterns. In fact, there is an entire movement of the "antipattern" which makes two points:
- Patterns should be referenced rather than repeated, and are therefore not patterns.
- Most patterns are replaced by features within programming languages.
These points are exemplified in the last paragraph of Paul Graham's Revenge of the Nerds. However, design patterns do have their place even if it is for merely being examples of good design.
Several concur on this thought too. Steve Rowe claims that every programmer should study design patterns as examples of good design, rather than for stocking up on design pattern books for reference; this point is also made by Jeff Atwood at Coding Horror.
In any case, the point is that design patterns are useful and that is what we will show here. First, let's look at some basic definitions of the three kinds of design patterns: creational, structural, and behavioral.
Creational
Creational design patterns look at how to best create objects. In other words, it's all about how objects are created. This deals with inheritance and prototyping.
Here is an example problem: you need to make an exact clone of an object at some point in your code. The prototype pattern looks at the best way to go about this (see example). Source Making's metaphor is to consider mitosis.
Structural
Structural design patterns look at how to establish the best relationships between objects. In other words, it's all about the structure of objects. This deals with the use of adapters and proxies.
Here is an example problem: you need to modify an interface (an API for example) to communicate in a different way. The adapter pattern builds an interface layer in between what is expected and the interface that already exists (see example). Consider the metaphor of an AC/DC converter in your car to power a laptop.
Behavioral
Behavioral design patterns look at how to best communicate between objects. In other words, it's all about the relationships between objects. This deals with handling states and delegation of responsibilities.
Here is an example problem: you need to change the state, or inventory, of an object based on how many times it is called. The state pattern looks at how to best produce a state-change effect (see example). Source Making's metaphor is to consider a vending machine that changes states based on inventory, the amount of change given, and the amount of change available in the machine.
Source Making's tutorial on design patterns is an excellent reference which includes explanations for specific patterns and examples given in different languages. Data & Object Factory also has a great listing of specific patterns with UML diagrams and sample code.
In my opinion, the "Gang of Four" are way beyond their time. Currently, we are still in the days of the digital equivalent of Ford's mass production assembly line which developed into its own discipline, industrial engineering. The study of design patterns is currently valid for helping developers think about how they approach problems, but the discipline will not mature until we reach the days when computers will be writing code. At that point, computers will have to make intelligent decisions that will be based on a library of these design patterns.
About the Author:
Joe Purcell is a technology virtuoso, cyberspace frontiersman, and connoisseur of Linux, Mac, and Windows alike.
|
|
|