Click here to Skip to main content
15,880,891 members
Articles / Programming Languages / C++
Tip/Trick

Simple Implementation of Abstract Factory Pattern using C++

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
5 Mar 2013CPOL 15.9K   9   3
It describes Abstract Factory Pattern which picks up the common hardware interface for different communication protocol.

Introduction

This post describes the simple implementation of Abstract Factory Pattern using C++.

Intent

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products".

Problem

We all know about PCI (Peripheral Component Interconnect) and USB ( Universal Serial Bus) communication protocol mechanisms for data transfer. Using this as an example, I have created an abstract interface for both protocols and access their respective methods.

UML Diagram

Image 1

Background

  1. Design Patterns using GOF
  2. C++ Programming
  3. Mentor - Mr.K.Babu Senior Software Engineer, Qmax Test Equipments Pvt. Ltd.

Code

C++
#include <iostream> 
using namespace std; 
// Abstract product 
class HWProduct
{
public:
    virtual void readData()=0;
    virtual void writeData()=0;
};
//Concrete USB Product
class USBProduct:public HWProduct
{
public:
    USBProduct() {}
    void readData()
    {
        cout << "USB Read"<<endl;
    }
    void writeData()
    {
        cout << "USB
        Write"<<endl;
    }
};

//Concrete PCI Product
class PCIProduct:public HWProduct
{
public:
    PCIProduct() {}
    void readData()
    {
        cout << "PCI Read"<<endl;
    }
    void writeData()
    {
        cout << "PCI
        Write"<<endl;
    }
};

// Abstract Factory
class HardwareFactory
{
public:
    virtual HWProduct* getProduct()=0;
};
// Concrete USB Factory
class USBFactory:public HardwareFactory
{
public:
    HWProduct* getProduct()
    {
        return new USBProduct();
    }
    ~USBFactory(){}
};
// Concrete PCI Factory
class PCIFactory:public HardwareFactory
{
public:
    HWProduct* getProduct()
    {
        return new PCIProduct();
    }
    ~PCIFactory(){}
};

class Application
{
public:
    Application(HardwareFactory* factory) 
    { 
        HWProduct* product = factory->getProduct(); 
        product->readData();
        product->writeData();
        delete product;
        delete factory;
    }
};

HardwareFactory* getHWFActory(short int FactoryID)
{
    if(FactoryID == 1)
        return new USBFactory();
    else
        return new PCIFactory();
} 

int main()
{
    Application
    application(getHWFActory(2));
    return 0;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
hsouza12-Mar-13 12:19
hsouza12-Mar-13 12:19 
GeneralRe: My vote of 1 Pin
Elangovan Deivasigamani12-Mar-13 18:08
Elangovan Deivasigamani12-Mar-13 18:08 
GeneralMy vote of 5 Pin
DinoLL++7-Mar-13 1:08
DinoLL++7-Mar-13 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.