C++ Interface
Here is the interface
-----------------------------------------------------
class IThing{
public:
virtual std::string getName() = 0;
virtual std::string getType() = 0;
virtual ~IThing(){};
};
-----------------------------------------------------
Here is the class that implemnts the interface
-----------------------------------------------------
class TheRealThing : public IThing{
public:
std::string getName();
std::string getType();
};
std::string TheRealThing::getName(){
return std::string("coke\n");
}
std::string TheRealThing::getType(){
return std::string("soft drink\n");
}
-----------------------------------------------------
Here is the class that is decoupled from
TheRealThing. This class need not change when a different
implementation of Ithing is provided.
Notice there are only references to the Ithing interface.
-----------------------------------------------------
class Decoupled{
public:
Decoupled();
~Decoupled();
void setThing(IThing* thing);
void getThingDetails();
private:
IThing* m_IThing;
};
Decoupled::Decoupled():
m_IThing(0)
{
}
Decoupled::~Decoupled(){
delete m_IThing;
}
void Decoupled::setThing(IThing* thing){
delete m_IThing;
m_IThing = thing;
}
void Decoupled::getThingDetails(){
if(m_IThing != 0){
std::cout<<m_IThing->getName();
std::cout<<m_IThing->getType();
}
}
-----------------------------------------------------
This is a test stub to run everything.
-----------------------------------------------------
int main(){
std::cout<<"main\n";
TheRealThing* aThing = new TheRealThing();
Decoupled aDecoupled;
aDecoupled.setThing(aThing);
aDecoupled.getThingDetails();
std::cout<<"main:END\n";
return 0;
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
C++_interface.tar