andrei_gavrila

Design Patterns - Observers and Handler

Nov 28th, 2021 (edited)
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  observer
  4. //
  5. //  Created by Andrei Gavrila on 28/11/2021.
  6. //  Copyright © 2021 Andrei Gavrila. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11. #include <algorithm>
  12.  
  13. class IToggleObserver
  14. {
  15. public:
  16.     virtual void onToggle(std::string objectName) = 0;
  17. };
  18.  
  19. class MyToggleObserver1: public IToggleObserver
  20. {
  21. public:
  22.     virtual void onToggle(std::string objectName)
  23.     {
  24.         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;
  25.     }
  26. };
  27.  
  28. class MyToggleObserver2: public IToggleObserver
  29. {
  30. public:
  31.     virtual void onToggle(std::string objectName)
  32.     {
  33.         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;
  34.     }
  35. };
  36.  
  37. class IObservableSwitch
  38. {
  39. public:
  40.     virtual std::string getObjectName() = 0;
  41. };
  42.  
  43. class IToggleHandler
  44. {
  45. public:
  46.     virtual int handleToggle(IObservableSwitch *object) = 0;
  47. };
  48.  
  49. class MyToggleHandler: public IToggleHandler
  50. {
  51. public:
  52.     virtual int handleToggle(IObservableSwitch *object)
  53.     {
  54.         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;
  55.  
  56.         return 0;
  57.     }
  58. };
  59.  
  60. class ObservableSwitch: public IObservableSwitch
  61. {
  62. private:
  63.     std::string objectName;
  64.  
  65.     std::vector<IToggleObserver *> toggleObservers;
  66.  
  67.     IToggleHandler *toggleHandler = nullptr;
  68.  
  69.     void notifyObservers()
  70.     {
  71.         std::for_each(toggleObservers.begin(), toggleObservers.end(), [&](IToggleObserver *observer) {
  72.             observer->onToggle(objectName);
  73.         });
  74.     }
  75. public:
  76.     ObservableSwitch(std::string objectName): objectName(objectName) {}
  77.  
  78.     virtual std::string getObjectName()
  79.     {
  80.         return objectName;
  81.     }
  82.  
  83.     void addObserver(IToggleObserver *observer)
  84.     {
  85.         toggleObservers.push_back(observer);
  86.     }
  87.  
  88.     void addHandler(IToggleHandler *handler)
  89.     {
  90.         toggleHandler = handler;
  91.     }
  92.  
  93.     int toggle()
  94.     {
  95.         std::cout << "Switch toggled, try to handle and notify the observers!" << std::endl;
  96.  
  97.         if (!toggleHandler)
  98.         {
  99.             std::cout << "No way to handle this, I have no handler!" << std::endl;
  100.  
  101.             return 1;
  102.         }
  103.  
  104.         int ret = toggleHandler->handleToggle(this);
  105.  
  106.         if (!ret)
  107.         {
  108.             std::cout << "Handled nicely!" << std::endl;
  109.  
  110.             notifyObservers();
  111.         } else {
  112.             std::cout << "That was not handled!" << std::endl;
  113.         }
  114.  
  115.         return ret;
  116.     }
  117. };
  118.  
  119. int main(int argc, const char * argv[])
  120. {
  121.     ObservableSwitch observableSwitch("Bedroom Light Switch");
  122.  
  123.     IToggleObserver *to = new MyToggleObserver1();
  124.  
  125.     observableSwitch.addObserver(new MyToggleObserver1());
  126.     observableSwitch.addObserver(new MyToggleObserver2());
  127.     observableSwitch.addObserver(to);
  128.     observableSwitch.addObserver(to);
  129.  
  130.     observableSwitch.toggle();
  131.  
  132.     observableSwitch.addHandler(new MyToggleHandler());
  133.  
  134.     observableSwitch.toggle();
  135.  
  136.     return 0;
  137. }
  138.  
  139. /*
  140.  
  141. Output:
  142.  
  143. Switch toggled, try to handle and notify the observers!
  144. No way to handle this, I have no handler!
  145. Switch toggled, try to handle and notify the observers!
  146. 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!
  147. Handled nicely!
  148. 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!
  149. 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!
  150. 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!
  151. 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!
  152. Program ended with exit code: 0
  153.  
  154. */
  155.  
Add Comment
Please, Sign In to add comment