© Dmitri Nesteruk 2018
Dmitri NesterukDesign Patterns in Modern C++https://doi.org/10.1007/978-1-4842-3603-1_3

3. Factories

Dmitri Nesteruk1 
(1)
St. Petersburg, Russia
 

I had a problem and tried to use Java, now I have a ProblemFactory.

–Old Java joke.

This chapter covers two GoF patterns at the same time: Factory Method and Abstract Factory. These patterns are closely related, so we’ll discuss them together.

Scenario

Let’s begin with a motivating example. Support you want to store information about a Point in Cartesian space. So you go ahead and implement something like this:

1   struct Point
2   {
3     Point(const float x, const float y)
4       : x{x}, y{y} {}
5     float x, y; // strictly Cartesian
6   };

So far, so good. But now, you also want to ...

Get Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.