Skip to content

Instantly share code, notes, and snippets.

@pazdera
Created August 15, 2011 07:37
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save pazdera/1145857 to your computer and use it in GitHub Desktop.
Save pazdera/1145857 to your computer and use it in GitHub Desktop.
Example of `adapter' design pattern in C++
/*
* 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;
}
@hugewave
Copy link

Very good!

@twboc
Copy link

twboc commented Mar 5, 2017

By far the best example I have seen!!!

@ardazeytin
Copy link

Very good example. Easy to understand. Thanks.

@rtrsocl
Copy link

rtrsocl commented Mar 19, 2019

Very good example. Thanks

@ramakrishnakondapalli
Copy link

very good example i have seen so far.Thanks a lot😊

@veejaynaikar
Copy link

Simplest and understandable illustration of Adapter pattern

@ivaylokardashev
Copy link

Very clear example thx you

@AhmedAbouali
Copy link

Nice Example

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