Abstract Factory Pattern

Abstract Factory Pattern is one of the creational design pattern.

According to Gang Of Four intent of Abstract Factory Pattern is to Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

SAMPLE CODE:

#include <iostream>

using namespace std;

enum DBType { MYSQL, ORACLE, REDIS, MANGODB };

class Database {
public:
    virtual void read() = 0;
    virtual void write() = 0;
};

class MysqlDB : public Database {
    void read() {
        cout << "Read data from MYSQL RDBMS Database" << endl;
    }
    void write() {
        cout << "Write data to MYSQL RDBMS Database" << endl;
    }
};

class OracleDB : public Database {
    void read() {
        cout << "Read data from ORACLE RDBMS Database" << endl;
    }
    void write() {
        cout << "Write data to ORACLE RDBMS Database" << endl;
    }
};

class RedisDB : public Database {
    void read() {
        cout << "Read data from REDIS NOSQL Database" << endl;
    }
    void write() {
        cout << "Write data to REDIS NOSQL Database" << endl;
    }
};

class MangoDB : public Database {
    void read() {
        cout << "Read data from MANGODB NOSQL Database" << endl;
    }
    void write() {
        cout << "Write data to MANGODB NOSQL Database" << endl;
    }
};

class DataBaseFactory {
public:
    virtual Database * getDatabase(DBType choice) = 0;
};

class RDBMSFactory : public DataBaseFactory {
    Database * getDatabase(DBType choice) {
        Database * DBObject;
        if (choice == MYSQL) {
            DBObject = new MysqlDB();
        }
        else if (choice == ORACLE) {
            DBObject = new OracleDB();
        }
        else {
            DBObject = NULL;
        }
        return DBObject;
    }
};

class NOSQLFactory : public DataBaseFactory {
    Database * getDatabase(DBType choice) {
        Database * DBObject;
        if (choice == REDIS) {
            DBObject = new RedisDB();
        }
        else if (choice == MANGODB) {
            DBObject = new MangoDB();
        }
        else {
            DBObject = NULL;
        }
        return DBObject;
    }
};

int main()
{
    DataBaseFactory * rdbmsObject, * nosqlObject;
    Database * databaseObject;

    rdbmsObject = new RDBMSFactory();
    databaseObject = rdbmsObject->getDatabase(ORACLE);
    databaseObject->read();
    databaseObject->write();
    cout << "-----------------------------------------" << endl;
    nosqlObject = new NOSQLFactory();
    databaseObject = nosqlObject->getDatabase(REDIS);
    databaseObject->read();
    databaseObject->write();

    return 0;
}


OUTPUT:

Read data from ORACLE RDBMS Database
Write data to ORACLE RDBMS Database
-----------------------------------------
Read data from REDIS NOSQL Database
Write data to REDIS NOSQL Database


Factory Method Pattern

Factory Method pattern comes under creational pattern.

According to Gang Of Four intent of Factory Method pattern is to Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

SAMPLE CODE:

#include <iostream>

using namespace std;

enum LaptopType { APPLE, DELL, HP };

class Laptop {
public:
    virtual void info() = 0;
};

class Apple : public Laptop {
    void info() {
        cout << "Apple Laptop Approved" << endl;
    }
};

class Dell : public Laptop {
    void info() {
        cout << "Dell Laptop Approved" << endl;
    }
};

class Hp : public Laptop {
    void info() {
        cout << "HP Laptop Approved" << endl;
    }
};

Laptop * getLaptopFactory(LaptopType choice)
{
    Laptop * laptopObj;
    if (choice == APPLE) {
        laptopObj = new Apple();
    }
    else if (choice == DELL) {
        laptopObj = new Dell();
    }
    else if (choice == HP) {
        laptopObj = new Hp();
    }
    else {
        laptopObj = NULL;
    }
    return laptopObj;
}

int main()
{
    Laptop * emp1Laptop;
    emp1Laptop = getLaptopFactory(DELL);
    emp1Laptop->info();

    Laptop * emp2Laptop;
    emp2Laptop =  getLaptopFactory(APPLE);
    emp2Laptop->info();
}

OUTPUT:

Dell Laptop Approved
Apple Laptop Approved

Singleton Design Pattern

Singleton pattern is one of the creational pattern.

According to GoF intent of Singleton pattern is to Ensure a class only has one instance, and provide a global point of access to it.


Code :

#include <iostream>

using namespace std;

class Logger {
public:
    static Logger* getInstance();

private:
    Logger(){}                   //Made private don't to create object via constructor
    Logger(Logger *);            //copy constructor is private so it can't be copied
    Logger* operator=(Logger *); //Assignment operator is private

    static Logger *instance;
};

Logger* Logger::instance = NULL;

Logger* Logger::getInstance() {
    if (instance == NULL) {
        instance = new Logger();
        cout << "Creating New Object "<< endl;
    } else {
        cout << "Utilizing Existing Object " << endl;
    }
    return instance;
}

int main()
{
    Logger *inst1 = Logger::getInstance();

    Logger *inst2 = Logger::getInstance();

    Logger *inst3 = Logger::getInstance();

    return 0;
}

Output:

Creating New Object
Utilizing Existing Object
Utilizing Existing Object