Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // observer
- //
- // Created by Andrei Gavrila on 28/11/2021.
- // Copyright © 2021 Andrei Gavrila. All rights reserved.
- //
- #include <iostream>
- #include <vector>
- #include <algorithm>
- class IToggleObserver
- {
- public:
- virtual void onToggle(std::string objectName) = 0;
- };
- class MyToggleObserver1: public IToggleObserver
- {
- public:
- virtual void onToggle(std::string objectName)
- {
- std::cout << "I am the " << static_cast<void*>(this) << " observer of type MyToggleObserver1 and I was told that a toggle was performed on " << objectName << ". I will do something about this!" << std::endl;
- }
- };
- class MyToggleObserver2: public IToggleObserver
- {
- public:
- virtual void onToggle(std::string objectName)
- {
- std::cout << "I am the " << static_cast<void*>(this) << " observer of type MyToggleObserver2 and I was told that a toggle was performed on " << objectName << ". I will simply ignore it as I don't want to do anything!" << std::endl;
- }
- };
- class IObservableSwitch
- {
- public:
- virtual std::string getObjectName() = 0;
- };
- class IToggleHandler
- {
- public:
- virtual int handleToggle(IObservableSwitch *object) = 0;
- };
- class MyToggleHandler: public IToggleHandler
- {
- public:
- virtual int handleToggle(IObservableSwitch *object)
- {
- std::cout << "I am the " << static_cast<void*>(this) << " handler of type MyToggleHandler and I was told that I need to perform a toogle on " << object->getObjectName() << ". I'm toggling the power!" << std::endl;
- return 0;
- }
- };
- class ObservableSwitch: public IObservableSwitch
- {
- private:
- std::string objectName;
- std::vector<IToggleObserver *> toggleObservers;
- IToggleHandler *toggleHandler = nullptr;
- void notifyObservers()
- {
- std::for_each(toggleObservers.begin(), toggleObservers.end(), [&](IToggleObserver *observer) {
- observer->onToggle(objectName);
- });
- }
- public:
- ObservableSwitch(std::string objectName): objectName(objectName) {}
- virtual std::string getObjectName()
- {
- return objectName;
- }
- void addObserver(IToggleObserver *observer)
- {
- toggleObservers.push_back(observer);
- }
- void addHandler(IToggleHandler *handler)
- {
- toggleHandler = handler;
- }
- int toggle()
- {
- std::cout << "Switch toggled, try to handle and notify the observers!" << std::endl;
- if (!toggleHandler)
- {
- std::cout << "No way to handle this, I have no handler!" << std::endl;
- return 1;
- }
- int ret = toggleHandler->handleToggle(this);
- if (!ret)
- {
- std::cout << "Handled nicely!" << std::endl;
- notifyObservers();
- } else {
- std::cout << "That was not handled!" << std::endl;
- }
- return ret;
- }
- };
- int main(int argc, const char * argv[])
- {
- ObservableSwitch observableSwitch("Bedroom Light Switch");
- IToggleObserver *to = new MyToggleObserver1();
- observableSwitch.addObserver(new MyToggleObserver1());
- observableSwitch.addObserver(new MyToggleObserver2());
- observableSwitch.addObserver(to);
- observableSwitch.addObserver(to);
- observableSwitch.toggle();
- observableSwitch.addHandler(new MyToggleHandler());
- observableSwitch.toggle();
- return 0;
- }
- /*
- Output:
- Switch toggled, try to handle and notify the observers!
- No way to handle this, I have no handler!
- Switch toggled, try to handle and notify the observers!
- I am the 0x1005665e0 handler of type MyToggleHandler and I was told that I need to perform a toogle on Bedroom Light Switch. I'm toggling the power!
- Handled nicely!
- I am the 0x100568760 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
- I am the 0x10053d880 observer of type MyToggleObserver2 and I was told that a toggle was performed on Bedroom Light Switch. I will simply ignore it as I don't want to do anything!
- I am the 0x100568340 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
- I am the 0x100568340 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
- Program ended with exit code: 0
- */
Add Comment
Please, Sign In to add comment