|
| 1 | +// Rewritten example - Original here |
| 2 | +// https://en.wikipedia.org/wiki/Decorator_pattern#UML_class_and_sequence_diagram |
| 3 | + |
| 4 | +#include <memory> |
| 5 | +#include <iostream> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +/*! |
| 9 | + * @brief : Component - defines the interface of the |
| 10 | + * objects you want to provide additional layers |
| 11 | + * of behaviours to. |
| 12 | + */ |
| 13 | +class WebPage |
| 14 | +{ |
| 15 | +public: |
| 16 | + virtual void display(void) const = 0; |
| 17 | + virtual ~WebPage() = default; |
| 18 | +}; |
| 19 | + |
| 20 | +/*! |
| 21 | + * @brief : ConcreteComponent - The objects you want to |
| 22 | + * be able to add responsabilities to. |
| 23 | + */ |
| 24 | +class BasicWebPage : public WebPage |
| 25 | +{ |
| 26 | +public: |
| 27 | + void display(void) const override { std::cout << "Basic WEB page" << std::endl; } |
| 28 | + |
| 29 | + BasicWebPage(std::string&& p_html = "") : m_html(p_html) {} |
| 30 | + ~BasicWebPage()=default; |
| 31 | + |
| 32 | +private: |
| 33 | + std::string m_html; |
| 34 | +}; |
| 35 | + |
| 36 | +/*! |
| 37 | + * @brief : Decorator - Interface that conforms to WebPage interface |
| 38 | + * Keep a reference (in that case a std::unique_ptr) on the |
| 39 | + * WebPage object. |
| 40 | + */ |
| 41 | +class WebPageDecorator : public WebPage |
| 42 | +{ |
| 43 | +public: |
| 44 | + WebPageDecorator(std::unique_ptr<WebPage> webPage): _webPage(std::move(webPage)) {} |
| 45 | + ~WebPageDecorator()=default; |
| 46 | + |
| 47 | + /*! |
| 48 | + * @brief : Note that the "basic behaviour" is still realized by the wrappee. |
| 49 | + */ |
| 50 | + void display(void) const override { _webPage->display(); } |
| 51 | + |
| 52 | +private: |
| 53 | + std::unique_ptr<WebPage> _webPage; |
| 54 | +}; |
| 55 | + |
| 56 | +/*! |
| 57 | + * @brief : ConcreteDecorator1 - Implements the new behaviour |
| 58 | + * added to the wrappee. |
| 59 | + */ |
| 60 | +class AuthenticatedWebPage : public WebPageDecorator |
| 61 | +{ |
| 62 | +public: |
| 63 | + AuthenticatedWebPage(std::unique_ptr<WebPage> webPage): |
| 64 | + WebPageDecorator(std::move(webPage)) {} |
| 65 | + ~AuthenticatedWebPage()=default; |
| 66 | + |
| 67 | + /*! |
| 68 | + * @brief : New behaviour enhanced by the ConcreteDecorator |
| 69 | + */ |
| 70 | + void display(void) const override |
| 71 | + { |
| 72 | + // Could be in any order |
| 73 | + authenticateUser(); |
| 74 | + WebPageDecorator::display(); |
| 75 | + } |
| 76 | + |
| 77 | +private: |
| 78 | + void authenticateUser(void) const { std::cout << "authentification done" << std::endl; } |
| 79 | +}; |
| 80 | + |
| 81 | +/*! |
| 82 | + * @brief : ConcreteDecorator2 - Implements the new behaviour |
| 83 | + * added to the wrappee. |
| 84 | + */ |
| 85 | +class AuthorizedWebPage : public WebPageDecorator |
| 86 | +{ |
| 87 | +public: |
| 88 | + AuthorizedWebPage(std::unique_ptr<WebPage> webPage): |
| 89 | + WebPageDecorator(std::move(webPage)) {} |
| 90 | + ~AuthorizedWebPage()=default; |
| 91 | + |
| 92 | + void display(void) const override |
| 93 | + { |
| 94 | + // Once again, could be in any order |
| 95 | + WebPageDecorator::display(); |
| 96 | + authorizedUser(); |
| 97 | + } |
| 98 | + |
| 99 | +private: |
| 100 | + void authorizedUser(void) const { std::cout << "authorized done" << std::endl; } |
| 101 | +}; |
| 102 | + |
| 103 | +int main() |
| 104 | +{ |
| 105 | + std::unique_ptr<WebPage> myPage = std::make_unique<BasicWebPage>(); |
| 106 | + std::cout << " --- ConcreteComponent --- " << std::endl; |
| 107 | + myPage->display(); |
| 108 | + |
| 109 | + myPage = std::make_unique<AuthorizedWebPage >(std::move(myPage)); |
| 110 | + std::cout << " --- Added ConcreteDecorator1 --- " << std::endl; |
| 111 | + myPage->display(); |
| 112 | + |
| 113 | + myPage = std::make_unique<AuthenticatedWebPage>(std::move(myPage)); |
| 114 | + std::cout << " --- Added ConcreteDecorator2 --- " << std::endl; |
| 115 | + myPage->display(); |
| 116 | + |
| 117 | + std::cout << std::endl; |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments