Abstract Factory with Singleton

Some times it is required that Factory shall have single object then it is need to merge Singleton pattern with Factory pattern, here we implemented Same SOAP example with singleton and abstract factory.

Here we made sure that HLL and ULL factories must have one and only one object.


#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
{
                static HLLFactory* objHLLptr;
                HLLFactory()
                {
                }
public:
                static HLLFactory* getInstance()
                {
                        if(objHLLptr == NULL)
                        {
                                objHLLptr=new HLLFactory();
                        }

                       return objHLLptr;

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

};
HLLFactory* HLLFactory::objHLLptr=NULL;

class ULLFactory : public AbsSOAPFactory  //Concrete Factory
{
                static ULLFactory* objULLptr;
                ULLFactory()
                {

                }

public:
                static ULLFactory* getInstance()
                {
                        if(objULLptr == NULL)
                        {
                                objULLptr=new ULLFactory();
                        }
                        return objULLptr;

                }

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

ULLFactory* ULLFactory::objULLptr=NULL;
AbsSOAPFactory* AbsSOAPFactory::createFactory(string str)// Implementation of Static Function
{
        if(!str.compare("HLL"))
                return (HLLFactory::getInstance());

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


int main()
{
        //Client - Asking for baby soap of HLL
        AbsSOAPFactory* objHLLFactory= AbsSOAPFactory::createFactory("HLL");
        cout<<"\nValue of objHLLFactory = "<<objHLLFactory<<endl;
        AbsBabySoap* objHLLBabySoap=objHLLFactory-> createBabySoap();
        objHLLBabySoap->BabySoapProp();

        //Client - Asking for women soap of HLL
        //Creating object twice Just to check singleton
        AbsSOAPFactory* objHLLFactory_2= AbsSOAPFactory::createFactory("HLL");
        cout<<"\nValue of objHLLFactory_2 = "<<objHLLFactory_2<<endl;
        AbsWomenSoap* objHLLWomenSoap=objHLLFactory_2-> createWomenSoap();
        objHLLWomenSoap->WomenSoapProp();


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

        //Client - Asking for women soap of ULL
        // Creating object twice Just to check singleton
        AbsSOAPFactory* objULLFactory_2= AbsSOAPFactory::createFactory("ULL");
        cout<<"\nValue of objULLFactory_2 = "<<objULLFactory_2<<endl;
        AbsWomenSoap* objULLWomenSoap=objULLFactory_2-> createWomenSoap();
        objULLWomenSoap->WomenSoapProp();

        return 0;
}





Output

 Value of objHLLFactory = 0x1015e040

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


Value of objHLLFactory_2 = 0x1015e040

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


Value of objULLFactory = 0x1015e0a0

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


Value of objULLFactory_2 = 0x1015e0a0

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




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