Click here to Skip to main content
Click here to Skip to main content

Tagged as

Simple Implementation of Abstract Factory Pattern using C++

, 5 Mar 2013
Rate this:
Please Sign up or sign in to vote.
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

Background

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

Code

#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)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberhsouza12-Mar-13 12:19 
GeneralRe: My vote of 1 PinmemberElangovan Deivasigamani12-Mar-13 18:08 
GeneralMy vote of 5 PinmemberDinoLL++7-Mar-13 1:08 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 5 Mar 2013
Article Copyright 2013 by Elangovan Deivasigamani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid