Skip to content

techtrendings.com

Let's explore

Menu
Menu

Abstract Factory Pattern C++ Simplified

Posted on January 6, 2022January 8, 2022 by Avidlearner
Image Reference: Unsplash

THE ABSTRACT FACTORY PATTERN

A factory in real life constructs tangible objects, such as Furniture. Similarly, a factory in object
oriented programming constructs objects. When you use factories in your program, portions of
code that want to create a particular object ask the factory for an instance of the object instead of
calling the object constructor themselves. Abstract Factory pattern lets a class defer instantiation to
subclasses.

If we want to create a Table, we can write code like this

Furniture *ft = new Table;

But if we are going to make it work across other platform, we should write something like this

Furniture *ft = FurnitureFactory->createTableorChair();

#include <iostream>

using namespace std;

class Furniture
{
    public:
    virtual void create() = 0;
};

class Table: public Furniture
{
    public:
    void create()
    {
        cout<<"Create Table"<<endl;
    }
};
class Chair:public Furniture
{
    public:
    void create()
    {
        cout<<"Create Chair"<<endl;
    }
};

class Furniturefactory
{
    public:
    virtual Furniture* createTableorChair()=0;
   
};

class Tablefactory:public Furniturefactory
{
    public:
    Furniture* createTableorChair() 
    { 
      return new Table(); 
    }
};

class Chairfactory:public Furniturefactory
{
    public:
    Furniture* createTableorChair() 
    { 
     return new Chair();
    }
};

int main()
{
    Furniturefactory *furniturefactory;
    Furniture *table;
    furniturefactory = new Tablefactory();
    table = furniturefactory->createTableorChair();
    table->create();
    Furniture *chair;
    furniturefactory = new Chairfactory();
    chair = furniturefactory->createTableorChair();
    chair->create();
  
    return 0;
}


Output:
Create Table
Create Chair


The Furniturefactory object abstracts the process of creating Table .Furniturefactory is not limited to producing Furniture user can add more class according to the need in software.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme