Factory Design Pattern C++ - C++ Programming Tutorial
C++ Course / Design Patterns / Factory Design Pattern C++

Factory Design Pattern C++

BLUF: Mastering Factory Design Pattern C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Factory Design Pattern C++

C++ is renowned for its efficiency. Learn how Factory Design Pattern C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

In the realm of Software Development, Design Patterns offer reusable strategies for typical programming challenges. One prevalent design pattern in object-oriented programming is the Factory Design Pattern. It furnishes an interface within a superclass for producing objects, allowing subclasses the flexibility to alter the types of objects generated. This piece will delve into the Factory Design Pattern in C++, detailing its advantages and how to incorporate it.

The Factory Design Pattern in C++:

A design pattern known as the Factory Design Pattern is employed to provide a way for creating objects within a parent class while enabling subclasses to alter the kind of objects to be generated. This design pattern proves valuable in scenarios where there is a requirement to generate numerous objects of identical nature, yet the exact nature of the objects is indeterminate until the program is running. The Factory Design Pattern is realized through a factory method, which is a method responsible for delivering an object of a designated kind.

The Factory Design Pattern offers the following advantages:

  • Encapsulates the process of creating objects:

The Factory Design Pattern abstracts the process of creating objects, offering a centralized mechanism for object instantiation. This simplifies the alteration of object types without impacting other parts of the code.

  • Reduces code coupling:

The Factory Design Pattern separates the code that utilizes the objects from the code responsible for generating them. This separation enables modifications to the code using the objects without impacting the code responsible for object creation.

  • Offers versatility:

The Factory Design Pattern offers versatility when it comes to object creation. Subclasses have the ability to generate objects of varying types without necessitating modifications to the code that interacts with these objects.

Implementation of the Factory Design Pattern in C++:

The Factory Design Pattern is executed through a factory method. A factory method is a function that generates and provides an instance of the designated class. This method is commonly declared in a parent class and is redefined in child classes to produce instances of varying types.

The upcoming illustration showcases the Factory Design Pattern implemented in C++.

C++ Code:

Example

#include <iostream>

class Animal {
public:
    virtual void speak() = 0;
    static Animal* createAnimal(int type);
};

class Dog : public Animal {
public:
    void speak() {
        std::cout << "Woof!" << std::endl;
    }
};

class Cat : public Animal {
public:
    void speak() {
        std::cout << "Meow!" << std::endl;
    }
};

Animal* Animal::createAnimal(int type) {
    if (type == 1) {
        return new Dog();
    } else if (type == 2) {
        return new Cat();
    } else {
        return nullptr;
    }
}

int main() {
    Animal* dog = Animal::createAnimal(1);
    dog->speak();

    Animal* cat = Animal::createAnimal(2);
    cat->speak();

    return 0;
}

Explanation:

In this instance, we have an Animal base class and two derived classes, Dog and Cat. The Animal base class contains an abstract speak function, which is implemented in the derived classes Dog and Cat. Moreover, the Animal base class features a static createAnimal function responsible for generating and providing an instance of the designated class type.

Within the main function, the createAnimal function is invoked to instantiate instances of the Dog and Cat classes. Subsequently, the speak method of each instance is invoked to display the respective animal vocalization.

Conclusion:

The Factory Design Pattern is a creational technique that provides a mechanism for creating objects within a superclass while allowing subclasses to alter the types of objects generated. This pattern is beneficial when there is a requirement to instantiate numerous objects of a similar kind without prior knowledge of their types until runtime. The Factory Design Pattern is executed through a factory method, a function that yields an object of a designated type.

The Factory Design Pattern offers numerous advantages, such as encapsulating the creation of objects, reducing dependencies in code, and offering versatility in object creation. This design pattern is extensively utilized in software engineering, especially beneficial in extensive projects where object creation complexity and management challenges arise.

In C++, a common approach is to utilize a factory method, responsible for generating and providing an instance of the designated type. This factory method is commonly established in a base class and can be redefined in derived classes to produce instances of varying types.

Ultimately, the Factory Design Pattern proves to be a potent asset in software development, especially when dealing with intricate or dynamic object creation scenarios. Embracing the Factory Design Pattern can greatly enhance a programmer's repertoire and is a crucial aspect to contemplate when architecting software systems in C++ or any alternative programming language.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience