Factory Method Pattern
(Acknowledgements to WikiPedia)
In object oriented programming, the factory method pattern is a design pattern that uses factory methods to deal with the problem of creating objects without having to specify their exact class. Rather than by calling a constructor, this is done by calling a factory method to create an object. Factory methods can either be specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes. It is one of the 23 classic design patterns described in the book Design Patterns (often referred to as the Gang of Four or simply GoF) and is sub-categorized as a creational pattern.
Overview
The Factory Method design pattern solves problems like:
- How can an object be created so that subclasses can redefine its subsequent and distinct implementation?
- How can an object's instantiation be deferred to a subclass?
This pattern describes how to solve such problems:
- Define a factory method within the superclass that defers the object's creation to a subclass's factory method.
- Create an object by calling a factory method instead of directly calling a constructor.
This enables the writing of subclasses that can change the way an object is created (e.g. by redefining which class to instantiate).
Definition
Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.
(Gang Of Four)
Creating an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects. As shown in the C# example below, The factory method pattern can also rely on an Interface - in this case IPerson - to be implemented.
Example of Implementation in C++
This C++14 implementation is based on the pre C++98 implementation in the book Design Patterns.
#include <iostream> #include <memory> enum ProductId {MINE, YOURS}; // defines the interface of objects the factory method creates. class Product { public: virtual void print() = 0; virtual ~Product() = default; }; // implements the Product interface. class ConcreteProductMINE: public Product { public: void print() { std::cout << "this=" << this << " print MINE\n"; } }; // implements the Product interface. class ConcreteProductYOURS: public Product { public: void print() { std::cout << "this=" << this << " print YOURS\n"; } }; // declares the factory method, which returns an object of type Product. class Creator { public: virtual std::unique_ptr<Product> create(ProductId id) { if (ProductId::MINE == id) return std::make_unique<ConcreteProductMINE>(); if (ProductId::YOURS == id) return std::make_unique<ConcreteProductYOURS>(); // repeat for remaining products... return nullptr; } virtual ~Creator() = default; }; int main() { // The unique_ptr prevent memory leaks. std::unique_ptr<Creator> creator = std::make_unique<Creator>(); std::unique_ptr<Product> product = creator->create(ProductId::MINE); product->print(); product = creator->create(ProductId::YOURS); product->print(); }
The program output is:
this=0x6e5e90 print MINE this=0x6e62c0 print YOURS