CPP implements a simple observer mode

Time:2022-1-3
1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 
 7 using namespace std;
 8 
 9 class Observer{
10 public:
11     void update(){
12         cout<state = state;
25         notify();
26     }
27 private:
28     void notify(){
29         for(auto po : this->pobs_set){
30             po->update();
31         }
32     }
33     multiset pobs_set;
34     int state;
35 };
36 int main()
37 {
38     Observer o1,o2,o3;
39     Subject sub;
40     sub.regist(&o1);
41     sub.regist(&o2);
42     sub.regist(&o3);
43 
44     sub.change(12);
45 
46     cout<

The following Ref: http://c.biancheng.net/view/1390.html

Intent:Defines a one to many dependency between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.

Main solutions:The problem of notifying other objects of an object’s state change should be considered, and the ease of use and low coupling should be considered to ensure a high degree of cooperation.

When to use:When the state of an object (target object) changes, all dependent objects (observer object) will be notified and broadcast.

How to solve:Using object-oriented technology, this dependency can be weakened.

Key codes:In the abstract class, there is an ArrayList for observers.

Application example:1. During the auction, the auctioneer observes the highest bid price and then notifies other bidders to bid. 2. In the journey to the west, Wukong asks the Bodhisattva to subdue the red boy. The Bodhisattva sprinkles water on the ground to attract an old tortoise. The tortoise is the observer. He observes the Bodhisattva’s action of sprinkling water.

advantage:1. The observer and the observed are abstractly coupled. 2. Establish a trigger mechanism.

Disadvantages:1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all observers. 2. If there is a circular dependency between the observer and the observation target, the observation target will trigger a circular call between them, which may lead to system crash. 3. The observer model has no corresponding mechanism to let the observer know how the observed target object has changed, but only know that the observed target has changed.

Usage scenario:

  • An abstract model has two aspects, one of which depends on the other. These aspects are encapsulated in independent objects so that they can be changed and reused independently.
  • The change of one object will lead to the change of one or more other objects, and the coupling between objects can be reduced by not knowing how many objects will change.
  • An object must notify other objects without knowing who they are.
  • You need to create a trigger chain in the system. The behavior of object a will affect object B, and the behavior of object B will affect object c.. You can use the observer mode to create a chain trigger mechanism.

matters needing attention:1. Java already has support classes for observer mode. 2. Avoid circular references. 3. If the sequence is executed, an observer error will cause the system to jam, and the asynchronous mode is generally adopted.

 

Recommended Today

Sass color function

In this section, let’s take a look at the color function in sass. The color function can be divided into three parts: color setting, color acquisition and color operation. There are many color functions in sass. Let’s look at some commonly used color functions in sass: function describe rgb() Create a red green blue (RGB) […]