Author image

Factory Design Pattern


Difficulty:
1/5


Definition:

Define an interface for creating an object, but let subclasses decide which class to instantiate.

Description

The factory function is used to create an object of any type of subclass of a given class.
Instead of creating objects like this:

ObjectType* o = new ObjectType{args};

we create them like this:

ObjectType* o = newObjectType{args};

Where newObjectType is the factory function. Yes the factory is nothing more than a function for objects, typically a global function (not a method).

Design

  • Create your class hierarchy.
  • The factory function returns one of several possible classes that share a common base class.
  • It's best to make the target class hierarchy constructors protected and make the factory function a friend of the class hierarchy such that only the factory has access to these classes and can create objects from them.
  • The base class constructor can be public. Just remember to create your objects using the factory function

The factory design pattern makes a, sort of, virtual constructor possible as you'll notice in the code.

When to use it?

  • When we don't want to expose the object's creation logic, or any of the concrete subclasses to the client.
  • When all the potential classes are in the same subclass hierarchy, but we don't know ahead of time (at compile time) exactly what specific class we need.

Abstract Factory

The related abstract factory design pattern is again a function which is used to create an object of any type of related, or depedent subclasses. In other words it creates an object among a family of similar classes. I've never used this one but for the sake of completion I make a reference to it.

I used Windows 8.1 x86_64, Visual Studio 2017, C++17 to build the project.

Github

Github repository link.


0 likes