Javatpoint Logo
Javatpoint Logo

C++ Factory Pattern

The Factory Pattern is a design pattern used in object-oriented programming to create objects without exposing the instantiation logic to the client. In other words, the Factory Pattern provides an interface for creating objects in a super-class but allows the subclasses to alter the type of objects that will be created.

C++ is an object-oriented programming language that supports the Factory Pattern using polymorphism and abstract classes. To implement the Factory Pattern in C++, we first need to create an abstract base class that defines the common interface for all objects that can be created by the factory. This abstract base class is also called the "Product" interface.

Explanation

Let's take an example of a simple program that creates different types of animals. We can define our abstract base class "Animal" as follows:

The "Animal" class is an abstract base class that defines the common interface for all types of animals. In this case, the interface consists of a single method called "makeSound()". The method is declared as pure virtual, which means that it has no implementation in the base class and must be implemented by its subclasses. The destructor is also declared as virtual, which ensures that the objects of the derived classes are correctly destroyed when we delete them using a pointer to the base class.

Next, we can create different types of animals by creating concrete classes that implement the "Animal" interface. For example, let's create two concrete classes: "Cat" and "Dog":

Both "Cat" and "Dog" classes inherit from the "Animal" class and implement the "makeSound()" method.

Now we can create our factory class, which is responsible for creating objects of the "Animal" type. The factory class should be able to create objects of any type that derives from the "Animal" class.

The "AnimalFactory" class is an abstract base class that defines the interface for creating objects of the "Animal" type. The interface consists of a single method called "createAnimal()", which returns a pointer to an "Animal" object. The method is declared as pure virtual, which means that it has no implementation in the base class and must be implemented by its subclasses.

Now we can create concrete factory classes that implement the "AnimalFactory" interface and create objects of different types of animals. For example, let's create two concrete factory classes: "CatFactory" and "DogFactory":

Both "CatFactory" and "DogFactory" classes inherit from the "AnimalFactory" class and implement the "createAnimal()" method. The method creates a new object of the appropriate type (either "Cat" or "Dog") and returns a pointer to the base class "Animal".

The complete code is:

Output:

C++ Factory Pattern

Conclusion

Finally, we can use the factory pattern to create objects of different types of animals without knowing the specific implementation details of the objects. We can create a client class that uses the factory to create objects of the "Animal".


Next TopicFunctor in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA