Abstract Factory Pattern

It generally known as Factory to create Factories. It is best used when one need to create object of families related products/entities without specifying their implementation. Before we get into depth of topic let's understand few terms.

(1) Abstract:- Here abstract not used in data abstraction sense, rather it implies as an interface that a client can use. Abstract Factory Class means a interface that is used by client to access Factory Classes that have actually implemented (Concrete Class) the process and Abstract Product means interface a client shall used to get a specific type of product. In layman term Abstract is like front office/Reception of an organization.

(2)Concrete:- Concrete implies class/method where actually implementation take place. It is a place of factory where, actually machines are running.

Lets understand in layman term where we can use abstract factory. we have taken example of SOAP factory in Factory pattern now take an example that there are two factories HindustanLiver and Unitedliver making soaps. Now client just go to factory and ask for soap of specific type then abstract factory is used.

Formally
- system dealing with multiple families of product.
- provide interface to class of library of products, hides implementation of library.




Participant
1) Abstract Factory : act as an interface to client for all kinds of factory.
2) Concrete Factory : Implements methods in abstract factory to create product.
3) Abstract Product : interface for type of product a factory is going to produces
4) Concrete Product : Implements methods given Abstract Product to meet client requirement for product specification
5) Client : uses Abstract Factory to create a specific product with the help of Abstract Product


Implementation in c++
As we already said many times that implementation my differ slightly because of language of code but basic idea behind shall remain same. Here in c++ we shall achieve abstract (interface) behaviour of a class by declaring pure virtual function in it once pure virtual function is created then creating object of object class is not possible hence it shall only act as parent/base class.



#include<iostream>
using namespace std;
class AbsBabySoap //Abstract Product
{
public:
        virtual void BabySoapProp()=0;
};

class HLLBabySoap : public AbsBabySoap //Concrete Product
{
public:
        void BabySoapProp()
        {
           cout<<"\n BabySoap (HLL) \n - Soft Fragrance\n - No Tears \n - Price = Rs.40/-\n\n ";
        }
};

class ULLBabySoap : public AbsBabySoap  //Concrete Product
{
public:
        void BabySoapProp()
        {
          cout<<"\n BabySoap (ULL) \n - Rosy Fragrance\n - Moisturizer \n - Price = Rs.25/-\n\n ";
        }
};

class AbsWomenSoap  //Abstract Product
{
public:
        virtual void WomenSoapProp()=0;
};

class HLLWomenSoap : public AbsWomenSoap //Concrete Product
{

public:
        void WomenSoapProp()
        {
          cout<<"\n WomenSoap (HLL) \n - Soft Fragrance\n - Germ Protection \n - Price = Rs.30/- \n\n ";
        }
};

class ULLWomenSoap : public AbsWomenSoap //Concrete Product
{

public:
        void WomenSoapProp()
        {
          cout<<"\n WomenSoap (ULL) \n - Rosy Fragrance\n - Germ Protection \n - Price = Rs.40/- \n\n";
        }
};

class AbsSOAPFactory //Abstract Factory
{

public:
        virtual AbsWomenSoap* createWomenSoap()=0;
        virtual AbsBabySoap* createBabySoap()=0;

        //Below function is created to make factory object in
        //modular form otherwise same can be done explicity
        //in main. But as AbsSOAPFactory is the only place
        // to which client is going to talk then it is better
        // to have a function to create object of factories
        //it is declared as STATIC bcz we can't create object
        static AbsSOAPFactory* createFactory(string str);
};

class HLLFactory : public AbsSOAPFactory //Concrete Factory
{

public:
                AbsWomenSoap* createWomenSoap()
                {
                        return new HLLWomenSoap();
                }
                AbsBabySoap* createBabySoap()
                {
                        return new HLLBabySoap();
                }

};
class ULLFactory : public AbsSOAPFactory  //Concrete Factory
{

public:
                AbsWomenSoap* createWomenSoap()
                {
                        return new ULLWomenSoap();
                }
                AbsBabySoap* createBabySoap()
                {
                        return new ULLBabySoap();
                }
};


AbsSOAPFactory* AbsSOAPFactory::createFactory(string str)// Implementation of Static Function
{
        if(!str.compare("HLL"))
                return new HLLFactory();

        if(!str.compare("ULL"))
                return new ULLFactory();
}


int main()
{
              //Client uses Abstract Factory to create specific product
       //with the help of Abstract Product. Here we created pointers of
       //Abstract Factory and Abstract Product only.
 
        //Client - Asking for baby soap of HLL
        AbsSOAPFactory* objHLLFactory= AbsSOAPFactory::createFactory("HLL");
        AbsBabySoap* objHLLBabySoap=objHLLFactory-> createBabySoap();
        objHLLBabySoap->BabySoapProp();

        //Client - Asking for women soap of HLL
        AbsWomenSoap* objHLLWomenSoap=objHLLFactory-> createWomenSoap();
        objHLLWomenSoap->WomenSoapProp();


        //Client - Asking for BABY soap of ULL
        AbsSOAPFactory* objULLFactory= AbsSOAPFactory::createFactory("ULL");
        AbsBabySoap* objULLBabySoap=objULLFactory-> createBabySoap();
        objULLBabySoap->BabySoapProp();

        //Client - Asking for women soap of ULL
        AbsWomenSoap* objULLWomenSoap=objULLFactory-> createWomenSoap();
        objULLWomenSoap->WomenSoapProp();

        return 0;
}


Output

 BabySoap (HLL)
 - Soft Fragrance
 - No Tears
 - Price = Rs.40/-


 WomenSoap (HLL)
 - Soft Fragrance
 - Germ Protection
 - Price = Rs.30/-


 BabySoap (ULL)
 - Rosy Fragrance
 - Moisturizer
 - Price = Rs.25/-


 WomenSoap (ULL)
 - Rosy Fragrance
 - Germ Protection
 - Price = Rs.40/-

Benefits
    - If we need to implement a new factory of SOAP then it is convenient and possible without disturbing already created factories


Abstract Factory with Singleton.


Your Comments /Suggestions and Questions are always welcome,  shall clarify with best of knowledge. So feel free to put Questions. 

No comments:

Post a Comment