Created
October 20, 2014 12:20
-
-
Save cengiz-io/49c382e0768f026c16b9 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <memory> | |
#include <string> | |
class Greeter { | |
public: | |
template <class T> | |
Greeter(T data) : self_(std::make_shared<Model<T>>(data)) {} | |
void greet(const std::string &name) const { | |
self_->greet(name); | |
} | |
private: | |
struct Concept { | |
virtual ~Concept() = default; | |
virtual void greet(const std::string &) const = 0; | |
}; | |
template <class T> | |
class Model : public Concept { | |
public: | |
Model(T data) : data_(data) {} | |
virtual void greet(const std::string &name) const override { | |
data_.greet(name); | |
} | |
private: | |
T data_; | |
}; | |
std::shared_ptr<const Concept> self_; | |
}; | |
class English { | |
public: | |
void greet(const std::string &name) const { | |
std::cout << "Good day " << name << ". How are you?\n"; | |
} | |
}; | |
class French { | |
public: | |
void greet(const std::string &name) const { | |
std::cout << "Bonjour " << name << ". Comment ca va?\n"; | |
} | |
}; | |
void greet_tom(const Greeter &g) { | |
g.greet("Tom"); | |
} | |
int main() { | |
greet_tom(English{}); | |
greet_tom(French{}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment