C++ Abstract Factory Pattern:
It provides an interface for creating families of related or dependent objects without specifying their concrete classes.
In layman language:
This is big factory, it creates many related objects.
Example:
Adidas sport wear is example of abstract factory, it creates many related object like shoes, sandal, jersey, Baseball, Basketball, Cricket bat and so on.
Class Diagram:
Objective:
- When we want to create multiple objects of same family.
- We are providing the interface of Abstract Factory to create object.
Technically UML explanation:
- Client is connected to AbstractFactory and AbstractPizza via association so it has pointer of both abstract class.
- Concrete factory is connected to concrete product via dependency so that Concrete factory is using pointer of concrete product inside function only.
Class Diagram of real example:
Source Code of real example in c++11:
/* * File:abstractFactory.cpp * This file is described the Abstract Factory Design Pattern with help of example * Author: Aplha Master * Date: 08 April 2021 */ ///////////////////////////////////Header files//////////////////////////////////// #include <iostream> #include<memory> // Abstract Pizz class class Pizza { public: virtual void Bake() = 0; }; class DominoPizza : public Pizza { public: void Bake() {std::cout<<"DominoPizza : Bake"<<std::endl;} }; // Abstract Burger class class Burger { public: virtual void Bake() = 0; }; class DominoBurger : public Burger { public: void Bake() {std::cout<<"DominoBurger : Bake"<<std::endl;} }; // Abstract Factory class AbstractFactory { public: virtual Pizza* CreatePizza() = 0; virtual Burger* CreateBurger() = 0; }; // Domino Factory class DominoFactory: public AbstractFactory { public: DominoFactory() {std::cout<<"DominoFactory is Created"<<std::endl;} ~DominoFactory() {} Pizza* CreatePizza() { return new DominoPizza; } Burger* CreateBurger() { return new DominoBurger; } }; //Client int main() { std::cout<<"Abstract Factory Design Pattern"<<std::endl; std::unique_ptr<AbstractFactory> upAf{ new DominoFactory }; std::unique_ptr<Pizza>pz{nullptr}; pz.reset(upAf->CreatePizza()); std::unique_ptr<Burger> bg{upAf->CreateBurger()}; pz->Bake(); bg->Bake(); return 0; }
Output:
Abstract Factory Design Pattern DominoFactory is Created DominoPizza : Bake DominoBurger : Bake
Source Code of real example in c++:
/* * File:abstractFactory.cpp * This file is described the Abstract Factory Design Pattern with help of example * Author: Aplha * Date: 03 Jan 2021 */ ///////////////////////////////////Header files//////////////////////////////////// #include <iostream> // Pizz class Pizza { public: virtual void Bake() = 0; }; class DominoPizza : public Pizza { public: void Bake() {std::cout<<"DominoPizza : Bake"<<std::endl;} }; // Burger class Burger { public: virtual void Bake() = 0; }; class DominoBurger : public Burger { public: void Bake() {std::cout<<"DominoBurger : Bake"<<std::endl;} }; // Abstract Factory class AbstractFactory { public: virtual Pizza* CreatePizza() = 0; virtual Burger* CreateBurger() = 0; }; // Domino Factory class DominoFactory: public AbstractFactory { public: DominoFactory() {std::cout<<"DominoFactory is Created"<<std::endl;} ~DominoFactory() {} Pizza* CreatePizza() { return new DominoPizza; } Burger* CreateBurger() { return new DominoBurger; } }; //Client int main() { std::cout<<"Abstract Factory Design Pattern"<<std::endl; AbstractFactory* pAf = new DominoFactory; Pizza* pz = pAf->CreatePizza(); Burger* bg = pAf->CreateBurger(); pz->Bake(); bg->Bake(); return 0; }
Compilation command:
g++ -std=c++11 abstractFactory.cpp
Output:
Abstract Factory Design Pattern
DominoFactory is Created
DominoPizza : Bake
DominoBurger : Bake