fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. class IObserver {
  6. public:
  7. virtual ~IObserver(){};
  8. virtual void Update(const std::string &message_from_subject) = 0;
  9. };
  10.  
  11. class ISubject {
  12. public:
  13. virtual ~ISubject(){};
  14. virtual void Attach(IObserver *observer) = 0;
  15. virtual void Detach(IObserver *observer) = 0;
  16. virtual void Notify() = 0;
  17. };
  18.  
  19. /**
  20.  * The Subject owns some important state and notifies observers when the state
  21.  * changes.
  22.  */
  23.  
  24. class Subject : public ISubject {
  25. public:
  26. virtual ~Subject() {
  27. std::cout << "Goodbye, I was the Subject.\n";
  28. }
  29.  
  30. /**
  31.   * The subscription management methods.
  32.   */
  33. void Attach(IObserver *observer) override {
  34. list_observer_.push_back(observer);
  35. }
  36. void Detach(IObserver *observer) override {
  37. list_observer_.remove(observer);
  38. }
  39. void Notify() override {
  40. std::list<IObserver *>::iterator iterator = list_observer_.begin();
  41. HowManyObserver();
  42. while (iterator != list_observer_.end()) {
  43. (*iterator)->Update(message_);
  44. ++iterator;
  45. }
  46. }
  47.  
  48. void CreateMessage(std::string message = "Empty") {
  49. this->message_ = message;
  50. Notify();
  51. }
  52. void HowManyObserver() {
  53. std::cout << "There are " << list_observer_.size() << " observers in the list.\n";
  54. }
  55.  
  56. /**
  57.   * Usually, the subscription logic is only a fraction of what a Subject can
  58.   * really do. Subjects commonly hold some important business logic, that
  59.   * triggers a notification method whenever something important is about to
  60.   * happen (or after it).
  61.   */
  62. void SomeBusinessLogic() {
  63. this->message_ = "change message message";
  64. Notify();
  65. std::cout << "I'm about to do some thing important\n";
  66. }
  67.  
  68. private:
  69. std::list<IObserver *> list_observer_;
  70. std::string message_;
  71. };
  72.  
  73. class Observer : public IObserver {
  74. public:
  75. Observer(Subject &subject) : subject_(subject) {
  76. this->subject_.Attach(this);
  77. std::cout << "Hi, I'm the Observer \"" << ++Observer::static_number_ << "\".\n";
  78. this->number_ = Observer::static_number_;
  79. }
  80. virtual ~Observer() {
  81. std::cout << "Goodbye, I was the Observer \"" << this->number_ << "\".\n";
  82. }
  83.  
  84. void Update(const std::string &message_from_subject) override {
  85. message_from_subject_ = message_from_subject;
  86. PrintInfo();
  87. }
  88. void RemoveMeFromTheList() {
  89. subject_.Detach(this);
  90. std::cout << "Observer \"" << number_ << "\" removed from the list.\n";
  91. }
  92. void PrintInfo() {
  93. std::cout << "Observer \"" << this->number_ << "\": a new message is available --> " << this->message_from_subject_ << "\n";
  94. }
  95.  
  96. private:
  97. std::string message_from_subject_;
  98. Subject &subject_;
  99. static int static_number_;
  100. int number_;
  101. };
  102.  
  103. int Observer::static_number_ = 0;
  104.  
  105. void ClientCode() {
  106. Subject *subject = new Subject;
  107. Observer *observer1 = new Observer(*subject);
  108. Observer *observer2 = new Observer(*subject);
  109. Observer *observer3 = new Observer(*subject);
  110. Observer *observer4;
  111. Observer *observer5;
  112.  
  113. subject->CreateMessage("Hello World! :D");
  114. observer3->RemoveMeFromTheList();
  115.  
  116. subject->CreateMessage("The weather is hot today! :p");
  117. observer4 = new Observer(*subject);
  118.  
  119. observer2->RemoveMeFromTheList();
  120. observer5 = new Observer(*subject);
  121.  
  122. subject->CreateMessage("My new car is great! ;)");
  123. observer5->RemoveMeFromTheList();
  124.  
  125. observer4->RemoveMeFromTheList();
  126. observer1->RemoveMeFromTheList();
  127.  
  128. delete observer5;
  129. delete observer4;
  130. delete observer3;
  131. delete observer2;
  132. delete observer1;
  133. delete subject;
  134. }
  135.  
  136. int main() {
  137. ClientCode();
  138. return 0;
  139. }
  140.  
Success #stdin #stdout 0.01s 5552KB
stdin
Standard input is empty
stdout
Hi, I'm the Observer "1".
Hi, I'm the Observer "2".
Hi, I'm the Observer "3".
There are 3 observers in the list.
Observer "1": a new message is available --> Hello World! :D
Observer "2": a new message is available --> Hello World! :D
Observer "3": a new message is available --> Hello World! :D
Observer "3" removed from the list.
There are 2 observers in the list.
Observer "1": a new message is available --> The weather is hot today! :p
Observer "2": a new message is available --> The weather is hot today! :p
Hi, I'm the Observer "4".
Observer "2" removed from the list.
Hi, I'm the Observer "5".
There are 3 observers in the list.
Observer "1": a new message is available --> My new car is great! ;)
Observer "4": a new message is available --> My new car is great! ;)
Observer "5": a new message is available --> My new car is great! ;)
Observer "5" removed from the list.
Observer "4" removed from the list.
Observer "1" removed from the list.
Goodbye, I was the Observer "5".
Goodbye, I was the Observer "4".
Goodbye, I was the Observer "3".
Goodbye, I was the Observer "2".
Goodbye, I was the Observer "1".
Goodbye, I was the Subject.