Skip to content

Instantly share code, notes, and snippets.

@pazdera
Created August 15, 2011 19:13
Show Gist options
  • Select an option

  • Save pazdera/1147487 to your computer and use it in GitHub Desktop.

Select an option

Save pazdera/1147487 to your computer and use it in GitHub Desktop.

Revisions

  1. pazdera revised this gist Aug 18, 2011. No changes.
  2. pazdera created this gist Aug 15, 2011.
    121 changes: 121 additions & 0 deletions bridge.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    /*
    * Example of `bridge' design pattern
    * Copyright (C) 2011 Radek Pazdera
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    * You should have received a copy of the GNU General Public License
    * along with this program. If not, see <http://www.gnu.org/licenses/>.
    */

    #include <iostream>
    #include <string>

    /* Implemented interface. */
    class AbstractInterface
    {
    public:
    virtual void someFunctionality() = 0;
    };

    /* Interface for internal implementation that Bridge uses. */
    class ImplementationInterface
    {
    public:
    virtual void anotherFunctionality() = 0;
    };

    /* The Bridge */
    class Bridge : public AbstractInterface
    {
    protected:
    ImplementationInterface* implementation;

    public:
    Bridge(ImplementationInterface* backend)
    {
    implementation = backend;
    }
    };

    /* Different special cases of the interface. */

    class UseCase1 : public Bridge
    {
    public:
    UseCase1(ImplementationInterface* backend)
    : Bridge(backend)
    {}

    void someFunctionality()
    {
    std::cout << "UseCase1 on ";
    implementation->anotherFunctionality();
    }
    };

    class UseCase2 : public Bridge
    {
    public:
    UseCase2(ImplementationInterface* backend)
    : Bridge(backend)
    {}

    void someFunctionality()
    {
    std::cout << "UseCase2 on ";
    implementation->anotherFunctionality();
    }
    };

    /* Different background implementations. */

    class Windows : public ImplementationInterface
    {
    public:
    void anotherFunctionality()
    {
    std::cout << "Windows :-!" << std::endl;
    }
    };

    class Linux : public ImplementationInterface
    {
    public:
    void anotherFunctionality()
    {
    std::cout << "Linux! :-)" << std::endl;
    }
    };

    int main()
    {
    AbstractInterface *useCase = 0;
    ImplementationInterface *osWindows = new Windows;
    ImplementationInterface *osLinux = new Linux;


    /* First case */
    useCase = new UseCase1(osWindows);
    useCase->someFunctionality();

    useCase = new UseCase1(osLinux);
    useCase->someFunctionality();

    /* Second case */
    useCase = new UseCase2(osWindows);
    useCase->someFunctionality();

    useCase = new UseCase2(osLinux);
    useCase->someFunctionality();

    return 0;
    }