Software Design Patterns in C++ & UML

OOP software is a complex game attempting to manage mutable state. There are well known patterns for common problems, you probably know them but don't know they had a fancy name. Here are some examples in C++.

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:

Image illustrating a classification of several Gang of Four design patterns into Creational, Structural, and Behavioural types, and indicating which are implemented as Classes or Objects

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.

Graph data structure with Gang of Four pattern names in boxes with arrows between them describing relationships between the patterns

An example: Fantasy-Chess in C++:

Screenshot of Simon's fantasy-chess repository on GitHub

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


Resources

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.

  1. Design Patterns by GoF (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) Richard Helm, Ralph Johnson, and John Vlissides) Richard Helm, Ralph Johnson, and John Vlissides)
  2. Head first design patterns by O’Reilly

Questions? I’d be humbled and happy to help.