The Abstract Factory pattern serves encapsulation to a group of individual factories without exposing the concrete classes. In this model, a generic interface of an abstract factory class is used to create the required concrete object separating the details of implementation of objects from their usage and composition. This design pattern is widely used in GUI applications where similar kind of GUI components needs to be created. For Design patterns basic explanation see (Design Patterns Simplified Version)
Generic model class diagram for Factory method design pattern can be explained as shown in image.
This design pattern has following major components:
1) Abstract Factory – This class declares the interface for the client which is used to create products.
2) Concrete Factory – This class has the implementation details of the concrete product object.
3) Abstract Product – This class declares the interface of a particular type of object.
4) Concrete Product – This class has the final concrete product details.
5) Client – This class uses the interfaces declared by Abstract Factory classes and creates final objects.
Abstract Factory Design Pattern Code Example:
Lets take an example of mobile phone creation example. Here for our example we have divided mobile
phones into two categories “Smart” and “Legacy” phones and we have taken three manufacturers which will act as factory classes. Class diagram for the solution of this problem is as shown in image.
Let’s have a look on below classes which will act as Abstract product classes and concrete product classes.
/* Abstract Base class */ class Mobile { public: virtual void getDetails() = 0; }; /* Concrete Mobile classes */ class SmartMobile:public Mobile { public: virtual void getDetails() { cout<<"SmartMobile created"<<endl; } }; class LegacyMobile:public Mobile { public: virtual void getDetails() { cout<<"LegacyMobile created"<<endl; } };
Let’s have a look on concrete factory classes which will be used by abstract factory class to create the product.
enum mobileManufacturer_e {SAMSUNG, APPLE, NOKIA}; /* Abstract Factory class*/ class MobileFactory { public: virtual SmartMobile* getSmart() = 0; virtual LegacyMobile* getLegacy() = 0; static MobileFactory* createMobileFactory(mobileManufacturer_e mobileManufacturer); }; /* Concrete Factory classes */ class Samsung:public MobileFactory { public: virtual SmartMobile* getSmart() { cout<<"Samsung "; return new SmartMobile(); } virtual LegacyMobile* getLegacy() { cout<<"Samsung "; return new LegacyMobile(); } }; class Apple:public MobileFactory { public: virtual SmartMobile* getSmart() { cout<<"Apple "; return new SmartMobile(); } virtual LegacyMobile* getLegacy() { cout<<"Apple "; return new LegacyMobile(); } }; class Nokia:public MobileFactory { public: virtual SmartMobile* getSmart() { cout<<"Nokia "; return new SmartMobile(); } virtual LegacyMobile* getLegacy() { cout<<"Nokia "; return new LegacyMobile(); } };
Let’s have a look on the Abstract factory class which will expose the interfaces to client to create product.
MobileFactory* MobileFactory::createMobileFactory(mobileManufacturer_e mobileManufacturer) { switch(mobileManufacturer) { case mobileManufacturer_e::SAMSUNG: return new Samsung(); case mobileManufacturer_e::APPLE: return new Apple(); case mobileManufacturer_e::NOKIA: return new Nokia(); default: cout<<"ERROR: Unknown Manufacturer selected"<<endl; } return NULL; }
Let’s see now how to use above classes to build the final product. Below code is sample “main” function code.
int main() { /*Samsung Mobile Creation*/ MobileFactory* factory = MobileFactory::createMobileFactory(mobileManufacturer_e::SAMSUNG); Mobile* mobile = NULL; mobile = factory->getSmart(); mobile->getDetails(); delete mobile; mobile = factory->getLegacy(); mobile->getDetails(); delete mobile; /* Apple Mobile Creation*/ factory = MobileFactory::createMobileFactory(mobileManufacturer_e::APPLE); mobile = factory->getSmart(); mobile->getDetails(); delete mobile; mobile = factory->getLegacy(); mobile->getDetails(); delete mobile; delete factory; }
Output of the above given example:
Samsung SmartMobile created Samsung LegacyMobile created Apple SmartMobile created Apple LegacyMobile created