Files
cpp-design-patterns-for-humans/examples/structural/adapter.cpp
Ian Dinwoodie 5a4975691f Rolling back use of typedef with smart pointers.
This is to reduce the obfuscation of the code.
2019-05-18 10:12:01 -04:00

73 lines
1008 B
C++

#include <iostream>
#include <memory>
class Lion
{
public:
virtual void roar(void) = 0;
};
class AfricanLion : public Lion
{
public:
void roar(void)
{
std::cout << "*African lion roar*" << std::endl;
}
};
class AsianLion : public Lion
{
public:
void roar(void)
{
std::cout << "*Asian lion roar*" << std::endl;
}
};
class Hunter
{
public:
void hunt(Lion& lion)
{
lion.roar();
}
};
class WildDog
{
public:
void bark(void)
{
std::cout << "*wild dog bark*" << std::endl;
}
};
class WildDogAdapter : public Lion
{
public:
WildDogAdapter(std::shared_ptr<WildDog> dog)
: dog_(dog)
{
}
void roar(void)
{
dog_->bark();
}
private:
std::shared_ptr<WildDog> dog_;
};
int main()
{
std::shared_ptr<WildDog> wildDog = std::make_shared<WildDog>();
WildDogAdapter wildDogAdapter(wildDog);
Hunter hunter;
hunter.hunt(wildDogAdapter); // Output: *wild dog bark*
return 0;
}