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

No comments:

Post a Comment