O'Reilly logo

Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design by Dmitri Nesteruk

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Dmitri Nesteruk 2018

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

3. Factories

Dmitri Nesteruk

(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 Point2   {3     Point(const float x, const float y)4       : x{x}, y{y} {}5     float x, y; // strictly Cartesian6   };

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required