andrei_gavrila

Design Patterns - Observer

Nov 28th, 2021
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 ObservableSwitch
  38. {
  39. private:
  40.     std::string objectName;
  41.  
  42.     std::vector<IToggleObserver *> toggleObservers;
  43.  
  44.     void notifyObservers()
  45.     {
  46.         std::for_each(toggleObservers.begin(), toggleObservers.end(), [&](IToggleObserver *observer) {
  47.             observer->onToggle(objectName);
  48.         });
  49.     }
  50. public:
  51.     ObservableSwitch(std::string objectName): objectName(objectName) {}
  52.  
  53.     void addObserver(IToggleObserver *observer)
  54.     {
  55.         toggleObservers.push_back(observer);
  56.     }
  57.  
  58.     void toggle()
  59.     {
  60.         std::cout << "Switch toggled, do something and notifying observers!" << std::endl;
  61.  
  62.         notifyObservers();
  63.     }
  64. };
  65.  
  66. int main(int argc, const char * argv[])
  67. {
  68.     ObservableSwitch observableSwitch("Bedroom Light Switch");
  69.  
  70.     IToggleObserver *to = new MyToggleObserver1();
  71.  
  72.     observableSwitch.addObserver(new MyToggleObserver1());
  73.     observableSwitch.addObserver(new MyToggleObserver2());
  74.     observableSwitch.addObserver(to);
  75.     observableSwitch.addObserver(to);
  76.  
  77.     observableSwitch.toggle();
  78.  
  79.     return 0;
  80. }
  81.  
  82. /*
  83.  
  84. Output:
  85.  
  86. Switch toggled, do something and notifying observers!
  87. I am the 0x10100d580 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
  88. I am the 0x101002e30 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!
  89. I am the 0x10100d160 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
  90. I am the 0x10100d160 observer of type MyToggleObserver1 and I was told that a toggle was performed on Bedroom Light Switch. I will do something about this!
  91.  
  92. */
  93.  
RAW Paste Data