An example Abstract Factory design pattern implementation in C++

I’m still working my way through the early stages of the Gang of Four’s Design Patterns: Elements of Reusable Object-Oriented Software, and this time I went to implement an abstract factory. Amazingly, I couldn’t find any fully working decent examples in C++, so I ended up taking the C# code from this dofactory post and converting it.

To be perfectly honest, it took me a while and a not unreasonable amount of swearing to get this up and running – but I guess that’s par for the course when you’re learning new things. Or at least that seems to be the case with me.

The Abstract Factory design pattern itself looks like this:

Abstract Factory UML Class Diagram

While the example code, involving continents, herbivores and carnivores goes like this:

That’s a lot of work to get the output:

But the point isn’t the output, it’s the structure of the classes and how it does what it does. Now I just have to learn when and how to apply the pattern to problems not involving imaginary animals… =D

2 thoughts on “An example Abstract Factory design pattern implementation in C++”

Leave a Reply

Your email address will not be published.