Skip to content

Instantly share code, notes, and snippets.

@pazdera
Created August 15, 2011 19:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save pazdera/1147487 to your computer and use it in GitHub Desktop.
Save pazdera/1147487 to your computer and use it in GitHub Desktop.
Example of `bridge' design pattern in C++
/*
* 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;
}
@s311354
Copy link

s311354 commented Dec 19, 2021

Thanks,very good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment