Created
August 15, 2011 07:37
-
-
Save pazdera/1145857 to your computer and use it in GitHub Desktop.
Example of `adapter' design pattern in C++
This file contains 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 `adapter' 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> | |
typedef int Cable; // wire with electrons :-) | |
/* Adaptee (source) interface */ | |
class EuropeanSocketInterface | |
{ | |
public: | |
virtual int voltage() = 0; | |
virtual Cable live() = 0; | |
virtual Cable neutral() = 0; | |
virtual Cable earth() = 0; | |
}; | |
/* Adaptee */ | |
class Socket : public EuropeanSocketInterface | |
{ | |
public: | |
int voltage() { return 230; } | |
Cable live() { return 1; } | |
Cable neutral() { return -1; } | |
Cable earth() { return 0; } | |
}; | |
/* Target interface */ | |
class USASocketInterface | |
{ | |
public: | |
virtual int voltage() = 0; | |
virtual Cable live() = 0; | |
virtual Cable neutral() = 0; | |
}; | |
/* The Adapter */ | |
class Adapter : public USASocketInterface | |
{ | |
EuropeanSocketInterface* socket; | |
public: | |
void plugIn(EuropeanSocketInterface* outlet) | |
{ | |
socket = outlet; | |
} | |
int voltage() { return 110; } | |
Cable live() { return socket->live(); } | |
Cable neutral() { return socket->neutral(); } | |
}; | |
/* Client */ | |
class ElectricKettle | |
{ | |
USASocketInterface* power; | |
public: | |
void plugIn(USASocketInterface* supply) | |
{ | |
power = supply; | |
} | |
void boil() | |
{ | |
if (power->voltage() > 110) | |
{ | |
std::cout << "Kettle is on fire!" << std::endl; | |
return; | |
} | |
if (power->live() == 1 && power->neutral() == -1) | |
{ | |
std::cout << "Coffee time!" << std::endl; | |
} | |
} | |
}; | |
int main() | |
{ | |
Socket* socket = new Socket; | |
Adapter* adapter = new Adapter; | |
ElectricKettle* kettle = new ElectricKettle; | |
/* Pluging in. */ | |
adapter->plugIn(socket); | |
kettle->plugIn(adapter); | |
/* Having coffee */ | |
kettle->boil(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice Example