From aa6f5dff887b3a05a6f8f4ff43bca3f6ec4e7f5c Mon Sep 17 00:00:00 2001 From: Ian Dinwoodie Date: Mon, 29 Apr 2019 20:14:57 -0400 Subject: [PATCH] Added bridge example source code. --- examples/structural/bridge.cpp | 109 +++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 examples/structural/bridge.cpp diff --git a/examples/structural/bridge.cpp b/examples/structural/bridge.cpp new file mode 100644 index 0000000..b01dc15 --- /dev/null +++ b/examples/structural/bridge.cpp @@ -0,0 +1,109 @@ +#include +#include +#include + +class Theme +{ + public: + typedef std::shared_ptr ptr_t; + virtual std::string getColor(void) = 0; +}; + +class WebPage +{ + public: + virtual std::string getContent(void) = 0; +}; + +class About : public WebPage +{ + public: + About(Theme::ptr_t theme) + : theme_(theme) + { + } + + std::string getContent(void) + { + return "About page in " + theme_->getColor(); + } + + private: + Theme::ptr_t theme_; +}; + +class Projects : public WebPage +{ + public: + Projects(Theme::ptr_t theme) + : theme_(theme) + { + } + + std::string getContent(void) + { + return "Projects page in " + theme_->getColor(); + } + + private: + Theme::ptr_t theme_; +}; + + +class Careers : public WebPage +{ + public: + Careers(Theme::ptr_t theme) + : theme_(theme) + { + } + + std::string getContent(void) + { + return "Careers page in " + theme_->getColor(); + } + + private: + Theme::ptr_t theme_; +}; + +class DarkTheme : public Theme +{ + public: + std::string getColor(void) + { + return "dark palette"; + } +}; + +class LightTheme : public Theme +{ + public: + std::string getColor(void) + { + return "light palette"; + } +}; + +class AquaTheme : public Theme +{ + public: + std::string getColor(void) + { + return "aqua palette"; + } +}; + +int main() +{ + Theme::ptr_t darkTheme = std::make_shared(); + About about(darkTheme); + Careers careers(darkTheme); + + // Output: About page in dark palette + std::cout << about.getContent() << std::endl; + // Output: Careers page in dark palette + std::cout << careers.getContent() << std::endl; + + return 0; +}