Factory Design pattern in C++

The factory design pattern is a creational design pattern and The sole  purpose of a creational design pattern is to create an object in the best possible way

As you might know, there are three kinds of design pattern

  1. Creational design pattern
  2. Structural design pattern
  3. Behavioral design

What is a factory design pattern

It is an abstraction between client class and implementation and  it will create an object for you rather then you initiate the object  directly

Suppose we have a book class and it has two subclasses such as math book and drawing book so we can provide type name as math and drawing

If we can provide the type to the factory class it will provide the object  of a subclass

The super class can be abstract or an interface class

Always a factory class will have a static method which will provide the instance of subclass based on the input type

Why we use a factory design pattern

Whenever we work on a project we use different  classes and all class should have low coupling and high cohesion

To achieve this low coupling between  classes we use a factory design pattern

It also hides creation logic from the client

Structure

Rendered by QuickLaTeX.com

Object.cpp

#include <iostream>
using namespace std;
class Building {
protected:
	string name;
	float price;
public:
	virtual void Windows()  = 0;
	virtual void Doors()  = 0;
	virtual void Reception_hall() = 0;
	virtual void apply_Label()    = 0;
	virtual void showBuilding()   = 0;
};
class School_building : public Building {
public:
	void Windows()  { cout << "School_building Windows" << endl;}
	void Doors()  { cout << "School_building Doors" << endl;}
	void Reception_hall() { cout << "School_building Reception_hall" << endl;}
	void apply_Label()      { cout << "School_building Label" << endl;  name = "School_building"; price = 1000;}
	void showBuilding () { cout << "Name: " << name << endl << "Price: " << price << endl;}
};
class University_building : public Building {
public:
	void Windows()  { cout << "University_building Windows" << endl;}
	void Doors()  { cout << "University_building Doors" << endl;}
	void Reception_hall() { cout << "University_building Reception_hall" << endl;}
	void apply_Label()      { cout << "University_building Label" << endl;  name = "University_building"; price = 20000;}
	void showBuilding () { cout << "Name: " << name << endl << "Price: " << price << endl;}
};
class Office_building : public Building {
public:
	void Windows()  { cout << "Office_building Windows" << endl;}
	void Doors()  { cout << "Office_building Doors" << endl;}
	void Reception_hall() { cout << "Office_building Reception_hall" << endl;}
	void apply_Label()      { cout << "Office_building Label" << endl;  name = "Office_building"; price = 30000;}
	void showBuilding () { cout << "Name: " << name << endl << "Price: " << price << endl;}
};

Building_factory.cpp

#include "Object.cpp"
class Building_Factory{
public:
	static Building * createBuilding(int type) {
	Building *Building = NULL;
	switch(type) {
		case 1:{
			Building = new School_building;
			break;
		}
		case 2:{
			Building = new University_building;
			break;
		}
		case 3:{
			Building = new Office_building;
			break;
		}
		default:{
			cout << "invalid toy type please re-enter type" << endl;
			return NULL;
		}
	}
	Building->Windows();
	Building->Doors();
	Building->Reception_hall();
	Building->apply_Label();
	return Building;
	}
};

Client.cpp

#include "Building_factory.cpp"
int main() {
	int type;
	while(1){
		cout << endl << "Enter type or Zero for exit" << endl;
		cin >> type;
		if(!type)
			break;
		Building *v = Building_Factory::createBuilding(type);
		if(v){
			v->showBuilding();
		}
	}
	cout << "Exit.." << endl;
	return 0;
}

Output

Enter type or Zero for exit
1
school_building windows
school_building Doors
school_building Reception_hall
school_building Label
Name:school_building
price:1000
Enter type or Zero for exit
2
university_building windows
university_building Doors
university_building Reception_hall
university_building Label
Name:university_building
price:20000
Enter type or Zero for exit
3
office_building windows
office_building Doors
office_building Reception_hall
office_building Label
Name:office_building
price:30000

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.