//============================================================================
// Name : Adapter.cpp
// Author : Andrew Selivanov
// Version : 1.0
// Copyright : Andrew Selivanov
// Description : GoF Adapter pattern implementation
//============================================================================
#include <iostream>
#define USE_ADAPTER
#ifndef USE_ADAPTER
class OrginSystem
{
public:
void Supply()
{
std::cout<<"Orgin:SupplyACup"<<std::endl;
}
};
class CapSystem
{
public:
void Supply()
{
std::cout<<"CapSystem:SupplyACap"<<std::endl;
}
};
class Adapter: public OrginSystem , CapSystem
{
public:
void Supply()
{
OrginSystem::Supply();
CapSystem::Supply();
std::cout<<"So the request has been satisfied!"<<std::endl;
}
};
int main()
{
Adapter adapter;
adapter.Supply();
}
#else
class OrginalSystem
{
public:
virtual void Supply()
{
std::cout<<"Orgin:SupplyACup"<<std::endl;
}
};
class CapSystem
{
public:
virtual void Supply()
{
std::cout<<"CapSystem:SupplyACap"<<std::endl;
}
};
class Adapter: public OrginalSystem , CapSystem
{
OrginalSystem* _os;
CapSystem* _cs;
public:
Adapter():_os(new OrginalSystem),_cs(new CapSystem)
{
}
void Supply()
{
_os->Supply();
_cs->Supply();
std::cout<<"So the request has been satisfied!"<<std::endl;
}
};
int main()
{
OrginalSystem* original = new Adapter();
original->Supply();
delete original;
}
#endif
Wednesday, September 12, 2007
GoF Adapter pattern implementation
Slightly modified version from: http://www.cppblog.com/cxl82116/archive/2007/04/21/22513.aspx
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment