Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Component {
- public:
- ~Component() {}
- virtual string Add() const = 0;
- virtual string Remove() const = 0;
- };
- class ConcreteComponent : public Component {
- public:
- string Add() const override {
- return "Ice-cream with: ";
- }
- string Remove() const override {
- return "Ice-cream without: ";
- }
- };
- class Decorator : public Component {
- protected:
- Component* component_;
- public:
- Decorator(Component* component) : component_(component) {
- }
- string Add() const override {
- return this->component_->Add();
- }
- string Remove() const override {
- return this->component_->Remove();
- }
- };
- class Syrup : public Decorator {
- public:
- Syrup(Component* component) : Decorator(component) {
- }
- string Add() const override {
- return Decorator::Add() + "Syrup ";
- }
- string Remove() const override {
- return Decorator::Remove() + "Syrup ";
- }
- };
- class Raspberry : public Decorator {
- public:
- Raspberry(Component* component) : Decorator(component) {
- }
- string Add() const override {
- return Decorator::Add() + "Raspberry ";
- }
- string Remove() const override {
- return Decorator::Remove() + "Raspberry ";
- }
- };
- void ClientCode(Component* component1, Component* component2) {
- cout << "1) I want : " << component1->Add();
- cout << "And " << component2->Remove();
- }
- int main() {
- Component* simple = new ConcreteComponent;
- Component* decorator1 = new Raspberry(simple);
- Component* decorator2 = new Syrup(decorator1);
- ClientCode(decorator2, decorator1);
- cout << "\n";
- delete simple;
- delete decorator1;
- delete decorator2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement