Proxy Pattern

Proxy Pattern comes under Structural Pattern.

According to Gang Of Four Intent of Proxy Pattern is to Provide a surrogate or placeholder for another object to control access to it.

UML DIAGRAM:




SAMPLE CODE:

#include <iostream>

using namespace std;

class Vote {
public:
    virtual void castVote(string name, int age) = 0;
};

class VoteSystem : public Vote {
public:
    void castVote(string name, int age)
    {
        cout << "NAME : " << name << ", AGE : " << age << " Casted Vote Successfully" << endl;
    }
};

class IndianVoteSystem : public Vote { // Proxy class
    Vote* voteOper;

public:
    IndianVoteSystem()
    {
        voteOper = new VoteSystem();
    }

    void castVote(string name, int age)
    {
        if (age >= 18) { // if age is more than 18 cast vote
            voteOper->castVote(name, age);
        }
        else {
            cout << age << " Years is Not Eligible Age to Cast Vote in India" << endl;
        }
    }
};

int main()
{
    string name;
    int age;

    Vote* indiaPoll = new IndianVoteSystem();

    cout << "Name ? "; cin >> name;
    cout << "Age  ? "; cin >> age;

    indiaPoll->castVote(name, age);

    return 0;

}


OUTPUT:

Name ? hirthik
Age  ? 16
16 Years is Not Eligible Age to Cast Vote in India

Name ? sharma
Age  ? 34
NAME : sharma, AGE : 34 Casted Vote Successfully

Name ? rahul
Age  ? 18
NAME : rahul, AGE : 18 Casted Vote Successfully

                                               INDEX - OTHER DESIGN PATTERNS

Bridge Pattern

Bridge Pattern is one of the Structural Design Pattern.

According to GoF intent of Bridge Pattern is to Decouple an abstraction from its implementation so that the two can vary independently.


SAMPLE CODE:

#include <iostream>

using namespace std;

class IColour {
public:
    virtual void paintColour() = 0;
};

class RedColour : public IColour {
public:
    void paintColour()
    {
        cout << "RED Colour Paint" << endl;
    }
};

class GreenColour : public IColour {
public:
    void paintColour()
    {
        cout << "GREEN Colour Paint" << endl;
    }
};

class IShape {
protected:
    IColour* colour;
public:
    IShape (IColour* colour) {
        this->colour = colour;
    }
    virtual void draw() = 0;
};

class Circle : public IShape {
public:
    Circle(IColour* c) : IShape(c) {
    }

    void draw()
    {
        cout << "Draw a Cirle With ";
        colour->paintColour();
    }
};

class Square : public IShape {
public:
    Square(IColour* c) : IShape(c) {
    }

    void draw()
    {
        cout << "Draw a Square With ";
        colour->paintColour();
    }
};

int main()
{
    RedColour redPaint;
    GreenColour greenPaint;
    IShape* circleImage, *squareImage;

    circleImage = new Circle(&redPaint);
    circleImage->draw();

    squareImage = new Square(&greenPaint);
    squareImage->draw();

    return 0;

}


OUTPUT:

Draw a Cirle With RED Colour Paint

Draw a Square With GREEN Colour Paint

                                               INDEX - OTHER DESIGN PATTERNS

Template Method Pattern

Template Method Pattern is one of the Behavioral design pattern.

According to Gang Of Four intent of Template Method Pattern is to Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


SAMPLE CODE:

#include <iostream>

using namespace std;

class CoffeeTemplate {
private:
    virtual void boilWater()
    {
        cout << "  * Boiling Water" << endl;
    }
    virtual void addMilk()
    {
        cout << "  * Adding Milk" << endl;
    }
    void addSugar()
    {
        cout << "  * Adding Sugar" << endl;
    }

    virtual void addCoffee() = 0;

public:
    void makeCoffee() /* Template Method */
    {
        cout << "Start Making Coffee" << endl;
        boilWater();
        addMilk();
        addCoffee();
        addSugar();
        cout << "Tasty Coffee Ready Enjoy!!!" << endl << endl;
    }
};

class NescafeeCoffee : public CoffeeTemplate {
    void addCoffee()
    {
        cout << "  * Adding Nescafee Coffee Podwer" << endl;
    }
};

class BruCoffee : public CoffeeTemplate {
    void addCoffee()
    {
        cout << "  * Adding Bru Coffee Powder" << endl;
    }
};

class SunriseCoffee : public CoffeeTemplate {
    void addCoffee()
    {
        cout << "  * Adding Sunrise Coffee Powder" << endl;
    }
};

int main()
{
    CoffeeTemplate* coffee = new BruCoffee();
    coffee->makeCoffee();

    coffee = new SunriseCoffee();
    coffee->makeCoffee();
    return 0;
}

OUTPUT:

Start Making Coffee
  * Boiling Water
  * Adding Milk
  * Adding Bru Coffee Powder
  * Adding Sugar
Tasty Coffee Ready Enjoy!!!

Start Making Coffee
  * Boiling Water
  * Adding Milk
  * Adding Sunrise Coffee Powder
  * Adding Sugar
Tasty Coffee Ready Enjoy!!!

                                               INDEX - OTHER DESIGN PATTERNS



Observer Pattern

Intent of Observer Design Pattern is to Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer pattern comes under Behavioral Pattern.

SAMPLE CODE:

#include <iostream>
#include <vector>
#include <unistd.h>
#include <time.h>

using namespace std;

class Subject;

class Observer {
public:
    string name;
    virtual void update() = 0;
};

class Subject {
private:
        vector <Observer*> observers;

public:
    void attach(Observer* obser)
    {
        observers.push_back(obser);
        cout << obser->name << " Suscribed To Get Notification" << endl << endl;
    }

    void detach(Observer* obser)
    {
        int observerCount = observers.size();
        for (int i = 0; i < observerCount; i++) {
            if (observers[i] == obser) {
                observers.erase(observers.begin() + i);
                cout << obser->name << " Unsuscribed From Receiving Notification" << endl;
                break;
            }
        }
    }

    void notify()
    {
        time_t now = time(NULL);
        struct tm * curtime = localtime(&now);
        cout << asctime(curtime);

        int observerCount = observers.size();
        for (int i = 0; i < observerCount; i++) {
            observers[i]->update();
        }
    }

};

class OfferPage : public Subject {
private:
    string  productName;
    double  actualPrice;
    double  offerPrice;

public:
    void setOffer(string product, int actual, int offer)
    {
        productName = product;
        actualPrice = actual;
        offerPrice = offer;
    }

    string getProduct()
    {
        return productName;
    }

    int getActualPrice()
    {
        return actualPrice;
    }

    int getOfferPrice()
    {
        return offerPrice;
    }
};

class GmailUsers : public Observer {
private:
    OfferPage* offerOfDay;

public:
    GmailUsers(OfferPage* offer)
    {
        name = "Gmailusers";
        offerOfDay = offer;
    }

    void update()
    {
        string product = offerOfDay->getProduct();
        double actPrice = offerOfDay->getActualPrice();
        double offPrice = offerOfDay->getOfferPrice();
        cout << "----> Sent TO Gmail Users" << endl;
        cout << "  Offer Of The Day - " << product << endl;
        cout << "    Actual Price   - " << actPrice << " USD" << endl;
        cout << "    Offer Price    - " << offPrice << " USD" << endl;
        cout << "    Savings        - " << (actPrice - offPrice) << " USD" << endl ;
    }
};

class YahooUsers : public Observer {
private:
    OfferPage* offerOfDay;
    
public:
    YahooUsers(OfferPage* offer)
    {
        name = "YahooUsers";
        offerOfDay = offer;
    }

    void update()
    {
        string product = offerOfDay->getProduct();
        double actPrice = offerOfDay->getActualPrice();
        double offPrice = offerOfDay->getOfferPrice();
        cout << "----> Sent TO Yahoo Users" << endl;
        cout << "  Offer Of The Day - " << product << endl;
        cout << "    Actual Price   - " << actPrice << " USD" << endl;
        cout << "    Offer Price    - " << offPrice << " USD" << endl;
        cout << "    Savings        - " << (actPrice - offPrice) << " USD" << endl << endl;
    }
};

int main()
{
    OfferPage todayOffer;
    todayOffer.setOffer("IPHONE 5S", 740, 725);

    Observer* gmailSuscriber = new GmailUsers(&todayOffer);
    todayOffer.attach(gmailSuscriber);
    Observer* yahooSuscriber = new YahooUsers(&todayOffer);
    todayOffer.attach(yahooSuscriber);

    todayOffer.notify();

    sleep(5);
    todayOffer.setOffer("Dell Laptop", 850, 839);
    todayOffer.notify();

    return 0;
}


OUTPUT:

Gmailusers Suscribed To Get Notification

YahooUsers Suscribed To Get Notification

Sun Oct  4 14:00:13 2015
----> Sent TO Gmail Users
  Offer Of The Day - IPHONE 5S
    Actual Price   - 740 USD
    Offer Price    - 725 USD
    Savings        - 15 USD
----> Sent TO Yahoo Users
  Offer Of The Day - IPHONE 5S
    Actual Price   - 740 USD
    Offer Price    - 725 USD
    Savings        - 15 USD

Sun Oct  4 14:00:18 2015
----> Sent TO Gmail Users
  Offer Of The Day - Dell Laptop
    Actual Price   - 850 USD
    Offer Price    - 839 USD
    Savings        - 11 USD
----> Sent TO Yahoo Users
  Offer Of The Day - Dell Laptop
    Actual Price   - 850 USD
    Offer Price    - 839 USD
    Savings        - 11 USD


                                               INDEX - OTHER DESIGN PATTERNS

Facade Pattern

According to Gang Of Four intent of Facade Design pattern is to Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

SAMPLE CODE:

#include <iostream>

using namespace std;

class PreprocessorSystem {
public:
    void doPreprocess()
    {
        cout << "--->Program In Preprocessing Stage" << endl;
    }
};

class CompilerSystem {
public:
    void doCompile()
    {
        cout << "--->Program In Compile Stage" << endl;
    }
};

class AssemblerSystem {
public:
    void doAssembly()
    {
        cout << "--->Program in Assembling Stage" << endl;
    }
};

class LinkerSystem {
public:
    void doLinking()
    {
        cout << "--->Program In Linking Stage" << endl;
    }
};

class CompilerFacade {
private:
    PreprocessorSystem  preprocessor;
    CompilerSystem      compiler;
    AssemblerSystem     assembler;
    LinkerSystem        linker;

public:
    void Compile(string programName)
    {
        cout << programName << " Compliation Started" << endl;
        preprocessor.doPreprocess();
        compiler.doCompile();
        assembler.doAssembly();
        linker.doLinking();
        cout << programName << " Compilation Finished" << endl;
    }
};

int main()
{
    CompilerFacade clientCompiler;
    clientCompiler.Compile("MYPROGRAM.cpp");

    return 0;
}   


OUTPUT:

MYPROGRAM.cpp Compliation Started
--->Program In Preprocessing Stage
--->Program In Compile Stage
--->Program in Assembling Stage
--->Program In Linking Stage
MYPROGRAM.cpp Compilation Finished

                            INDEX - OTHER DESIGN PATTERNS