Created
August 15, 2011 19:13
-
-
Save pazdera/1147487 to your computer and use it in GitHub Desktop.
Example of `bridge' design pattern in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
thanks.
Thanks,very good!
it will be good and more aligned to the pattern if the pointer to the ImplementationInterface can be moved to the AbstractInterface instead of its child classes , like this:
/* Forward Declaration */
class ImplementationInterface;
/* Implemented interface. */
class AbstractInterface
{
protected:
ImplementationInterface* implementation;
public:
virtual void someFunctionality() = 0;
};
Thanks,very good!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. It's useful.