There is massive value is writing throw away code, but there is also tons of value in writing code as if you had to maintain it for the rest of your life. Design patterns may seem simple at first glance, but the magnitude to which these patterns have affected the way I design is immesureable.
I present some common GoF design patterns (in an example project) that are now second nature to me, but at the time, took grueling, early hours of the morning to comprehend the structure and benefits of each one.
What are design patterns?
It's a generic solution to common design problems in software development.
Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice ~ Christopher Alexander
Patterns can be divided into creational, structural and behavioural types, and can apply to classes and/or objects.
- Creational patterns concern the process of object creation.
- Structural patterns deal with the composition of classes or objects.
- Behavioural patterns characterize the way that classes or objects interact and distribute responsibility.
Here's a breakdown of the GoF design patterns:
I love this example, straight from the GoF book of how the patterns are related to one another can complement each other. Often a pattern is used within another pattern to solve a larger or more generic problem.
An example: Fantasy-Chess in C++
The first time I encountered design patterns was in a university class while learning programming from the ground up with C++. It's a nice language if you stay away from most of the crazy things you can do with it that make your code practically unreadable.
We had to produce a command line game called "Fantasy-Chess", incorporating many design patterns such that it was extensible and maintainable. I've put up my code on github as an illustration for the patterns.
The repository includes unified modelling language diagrams (UML) of each pattern implemented, an explanation of the rationale and of course, the source.
Check it out here: Fantasy Chess Source
These two books were instumental in my understanding of these patterns. GoF for the density of knowledge packed into those pages, and Head first design patterns as a more practical approach which resonated with the way I learn.
(first) Design Patterns by GoF (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides)
(second) Head first design patterns by O'Reilly