Explanation of Abstract Factory Design Pattern in C++

Explanation of Abstract Factory Design Pattern in C++

Hello everyone, in this article we are going to talk about Abstract Factory Design Pattern which belongs to Creational Design Patterns on a C++ example with QT program.

Let's Begin.

Before We start to learn, First you should check out below article which I explained Creational Design Patterns with C# language :
https://thecodeprogram.com/explanation-of-creational-design-patterns

According to my Explanation of Creational Design Patterns article Abstract Factory Design Pattern is:
It is similar to Factory type. The differance between factory and abstract factory usage of interfaces. At factory type we are creating an interface for all similar classes. But in abstract factory we create interface for same types classes.
For Example we create interfaces for all types of vecihles but in abstract factory we create seperate interfaces for cars, aircrafts, trucks... this is the differance between factory and abstract factory types of creational design patterns. So if we need to work multi types objects we can use factory method. So do not need write so many if / else if blocks as factory as much in this type. It is also more flexible than factory type.
Below image you can see the schematic of our example : Schmatic of The Abstract Factory Design Pattern Example

Now Let's Code :)

I have created a console application in QT with C++ and I will declare all classes in main.c file. No need any external header files.
,
Do not forget to add the required header libraries to the project:

#include <QCoreApplication>
#include <iostream>
using namespace std;
First we have to create the base class all components are going to derive from this base class. So this class has to accomodate all requirements of needed classes.

class Vehicle
{
 public:
  virtual ~Vehicle();
  virtual void START_ENGINE() = 0;
  virtual void GET_READY() = 0;
};
Then we create for classes and two of them are same type the other two are the same type vehicles. We derived all of these class from above base class and so all of them have same functions as you can see below:

class Aircraft : public Vehicle
{
 public:
  void START_ENGINE() { cout << "Aircraft Engine Started" << endl; }
  void GET_READY() { cout << "Aircraft is ready for operation" << endl; }
};

class Drone : public Vehicle
{
 public:
    void START_ENGINE() { cout << "Drone Engine Started" << endl; }
    void GET_READY() { cout << "Drone is ready for operation" << endl; }
};

class Car : public Vehicle
{
 public:
    void START_ENGINE() { cout << "Car Engine Started" << endl; }
    void GET_READY() { cout << "Car is ready for operation" << endl; }
};

class Truck : public Vehicle
{
 public:
    void START_ENGINE() { cout << "Truck Engine Started" << endl; }
    void GET_READY() { cout << "Truck is ready for operation" << endl; }
};
And then we create our abstract factory class to declare vehicle type classes. Abstract factory class will derive the Type classes with same methods.

class Factory
{
 public:
 virtual ~Factory(){}
  virtual Vehicle *create_small_vehicle() = 0;
  virtual Vehicle *create_big_vehicle() = 0;
};
And here our concrete factory classes. These classes are going to create our vehicles according to user request.

class FliyngTypeFactory : public Factory
{
 public:
  Vehicle *create_big_vehicle()
  {
    return new Aircraft;
  }
  Vehicle *create_small_vehicle()
  {
    return new Drone;
  }
};

class GroundTypeFactory : public Factory
{
 public:
  Vehicle *create_small_vehicle()
  {
    return new Car;
  }
  Vehicle *create_big_vehicle()
  {
    return new Truck;
  }
};
Now all we need to call above classes from main function. I assume my user will wanna see all of type classes and I will show all of types classes. Below coe block you will see them.

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    system("title Factory Design Pattern Example - Thecodeprogram");
    cout << "Welcome Factory Design Pattern Example in QT C++" << endl << endl;

    Factory *factory;
    Vehicle * vec;

    factory = new FliyngTypeFactory;

    vec = factory->create_big_vehicle();
    vec->START_ENGINE();
    vec->GET_READY();

    cout << "--------------------------------------------------------------------------" << endl;

    vec = factory->create_small_vehicle();
    vec->START_ENGINE();
    vec->GET_READY();

    cout << "--------------------------------------------------------------------------" << endl;
     //Initialize other type of vehicles
    factory = new GroundTypeFactory;

    vec = factory->create_big_vehicle();
    vec->START_ENGINE();
    vec->GET_READY();

    cout << "--------------------------------------------------------------------------" << endl;

    vec = factory->create_small_vehicle();
    vec->START_ENGINE();
    vec->GET_READY();

    cout << "--------------------------------------------------------------------------" << endl;

    return a.exec();
}
Below image you can see the output of the example : Abstract Factory Design Pattern Example Output

That is all in this article.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...