DEV Community 👩‍💻👨‍💻

Cover image for Factory Method
José Gomes
José Gomes

Posted on

Factory Method

Design patterns should not be applied indiscriminately. Often they achieve flexibility and variability by introducing additional levels of indirection, and that can complicate a design and/or cost you some performance. A design pattern should only be applied when the flexibility it affords is actually needed - Erich Gamma


One of the biggest challenges in software development is how to write code that enables easy scalability, maintenance, and flexibility.

One solution for this problem is to use Design Pattern, a guideline of good practices to write clean and organized code, allowing anyone to easily understand and contribute.

Design Patterns can be divided into 4 categories:

  1. Creational: Object instantiation

  2. Structural: Class relationships and hierarchies

  3. Behavioral: Object intercommunication

The creational categories include five methods:

  1. Factory method

  2. Abstract method

  3. Builder method

  4. Prototype method

  5. Singleton method

Today we will focus on the Factory method!

What is Factory Method?

The factory method encapsulates the creation of related objects, providing an interface to create an object but defers the creation to the sub-class. This is one of the most methods used, the reason for that are the benefits:

  1. Creates objects based on runtime parameters

  2. Do not need to know which objects you will need

  3. Lets the subclasses instantiate the object instead of the min

  4. Easily add new products or change existing ones

  5. No need to make changes throughout

  6. Extensible to include objects

  7. Object generation located in one place

  8. Easily switch between factories


In the factory method, we have three structures:

  1. Class creator: Provide some default implementation of the factory method.

  2. Concrete creator: Override the factory method in order to change the resulting product's type.

  3. Concrete product: Provides various implementation of the product interface.


Here we will learn how to use a Factory Method, through a very simple example of a game, where we must spawn different types of rock, in the first moment, we will have two types of rock: Small and Mid. The way the code is written will be very easy to register a new type of rock(E.g: Big, Iron).

#include <iostream>

using namespace std;

//Class creator
class rock {
    char *getType(){
        return _type;

//Concrete product
class smallRock:public rock{
        //_type = "Small";
        cout << "\nSmall rock created" << endl;

//Concrete product
class midRock:public rock{
        //_type = "mid";
        cout<<"\nMid rock created" << endl;

/*Creator concrete*/ 
class rockFactoryCreator{
    rock *GetRock(){
        int choice;
        cout << "Select type of rock to make: " << endl;
        cout << "1: Small" << endl;
        cout << "2: Mid" << endl;
        cout << "Selection: ";
        cin >> choice;

        switch (choice)
        case 1:
            return new smallRock;
        case 2:
            return new midRock;
            cout << "Invalid Selection" << endl;
            return NULL;

int main()
    rockFactoryCreator rockCreator;
    rock *asteroid; 
    asteroid = rockCreator.GetRock();
    return 0;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👀 Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.