Factory Design Pattern

It is one of the widely used creational design pattern, based on run-time polymorphism principle of OOPs. It is called as Factory pattern because a creation class act as Factory. If we take an example to SOAP factory where we/Client just ask for Baby Soap, Men Soap, Women Soap without knowing the internal information of how these Soaps created. Client just interact with Factory for Soap, Factory ask for its choice, and returns required Item.

Formally Factory pattern works with the idea of delegating the object creation to subclasses and hides the details (which class has actually created an object) with the help of a class that act as Factory.

Where can we use Factory Pattern?
Following are some of major point where Factory pattern can be used
(1) When a Class can not presume that which object to create
(2) When a class wants to delegate responsibilities to subclasses





Factory Pattern Has following Participant
(1)Product :- Interface of Product that factory is going to use
(2)Concrete Product:- Implementation of Product interface
(3)Creator/Client:- Call Factory method to create Product object
(4)Concrete Creator/Factory :- Implements Factory Method and returns Product Object



Implementation

We have assumed that you have basic idea of inheritance and virtual keyword, if not we request you to just run below given code to without virtual keyword and then run with virtual keyword and see the difference.
Below example is self explanatory



#include<iostream>
using namespace std;
//(1)Product :- Interface of Product that factory is going to create
class Product
{
public:
        virtual void DisplayProduct ()
        {
        }

};
class Product1: public Product //(2)Concrete Product:- Implementation of Product interface
{
public:
        void DisplayProduct()
        {
                cout<<"\n Product1 is DisplayProduct";
        }
};
class Product2: public Product //(2)Concrete Product:- Implementation of Product interface
{
public:
        void DisplayProduct()
        {
               cout<<"\n Product2 is DisplayProduct";
        }
};
class Factory //(4)Concrete Creator:- Implements Factory Method and returns Product Object
{
public:
        Product* Manufacture(string ItemName)
        {
                if(!ItemName.compare("Product1"))
                        return new Product1();

                if(!ItemName.compare("Product2"))
                        return new Product2();
        }
};
int main() //(3) Creator/Client:- Call Factory method to create Product object
{
        //Main function acting as client
        //Client just need to tell Factory about
        //product details done below statement
        //objFactory->Manufacture("Product1");
        //Object of Product1 is created by Factory

        Factory* objFactory=new Factory();
        Product* prod1=objFactory->Manufacture("Product1");
        prod1->DisplayProduct();

        Product* prod2=objFactory->Manufacture("Product2");
        prod2->DisplayProduct();
}




Output:-
[TechSujhav- FactoryPattern]$ ./a.out
 Product1 is DisplayProduct
 Product2 is DisplayProduct


Benefits
- Enhancement is independent : For example now Factory wants to support one more product let say "Product3" then it need to add one more class with name Product3 and let it inherit class Product and implement function, major thing is that implementation of "Product3" is not impacting implementation of "Product1" and "Product2"

- Easy to change: If someone wants to change implementation of DisplayProduct of "Product2" then it is possible without change in implementation of same in "Product1"


Putting Factory pattern in practical situation that we discuss already where a client asks for SOAP



#include<iostream>
using namespace std;
//(1)Product :- Interface of Product that factory is going to create
class SOAP
{

public:
        virtual void Properties ()
        {
        }
};
class BabySoap: public SOAP //(2)Concrete Product:- Implementation of Product interface
{
public:
        void Properties()
        {
              cout<<"\n BabySoap \n - Soft Fragrance\n - No Tears \n\n ";    
        }
};
class WomenSoap: public SOAP //(2)Concrete Product:- Implementation of Product interface
{
public:
        void Properties()
        {
               cout<<"\n WomenSoap \n - Rosy Fragrance\n - Moisturizer \n\n ";
        }
};
class MenSoap: public SOAP //(2)Concrete Product:- Implementation of Product interface
{
public:
        void Properties()
        {
               cout<<"\n MenSoap \n - Long Lasting Fragrance\n - Germ Protection \n\n ";
        }
};

class SoapFactory //(4)Concrete Creator:- Implements Factory Method and returns Product Object
{
public:
        SOAP* Manufacture(string ItemName)
        {
                if(!ItemName.compare("MenSoap"))
                        return new MenSoap();

                if(!ItemName.compare("WomenSoap"))
                        return new WomenSoap();

                if(!ItemName.compare("BabySoap"))
                        return new BabySoap();
        }

};

int main()  //(3) Creator/Client:- Call Factory method to create Product object
{
        //Main function acting as client
        //Client just need to tell Factory about
        //product details done below statement
        //objFactory->Manufacture("Product1");
        //Object of Product1 is created by Factory
        SoapFactory* objFactory=new SoapFactory();
        SOAP* soap=objFactory->Manufacture("MenSoap");
        soap->Properties();

        //WomenSoap
        soap=objFactory->Manufacture("WomenSoap");
        soap->Properties();

        //BabySoap
        soap=objFactory->Manufacture("BabySoap");
        soap->Properties();
}

Output:-

[TechSujhav- FactoryPattern]$ ./a.out

 MenSoap
 - Long Lasting Fragrance
 - Germ Protection


 WomenSoap
 - Rosy Fragrance
 - Moisturizer


 BabySoap
 - Soft Fragrance
 - No Tears

PREV:: Is Singleton thread safe?

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

1 comment:

  1. This blog is truly useful to convey overhauled instructive undertakings over web which is truly examination. I discovered one fruitful case of this truth through this blog. I will utilize such data now.
    ไทย อิตาลี

    ReplyDelete