Different ways to monitor data changes

I have many classes in my application. Most of these classes store quite some data, and it is important that other modules in my application also "update" if the contents of one of the data classes change.

A typical way to do this is as follows:

void MyDataClass::setMember(double d)
{
m_member = d;
notifyAllObservers();
}

This is a good method if the term does not change frequently and the "observational classes" should be as fast as possible.

Another way to monitor changes is to:

void MyDataClass::setMember(double d)
{
setDirty();
m_member = d;
}

This is a good method if the member has changed many times, and the "observing classes" look at regular intervals in all "dirty" instances.

, . ( ), ( ) , .

- , ?

, ( ), ++.

+3
4

() , , , .

, , .

  • Push , .
  • Pull , .

, , 2 3, dirty..., . , , , , , , .

, , : .

  • GlobalObserver
  • GlobalObserver

, , , , , . .

Epoch 0       Epoch 1      Epoch 2
event1        event2       ...
...           ...

, ( , ) , . , , , , , ( ).

, , ( ). . , GlobalObserver - , . , , , ( ).

  • β†’ , !

, , (.. ), , ( ).

, , ( push ), ( ). , , . , , .

+5

. "push" "pull". .

A notifyAllObservers - push, - pull.

. , , , .

.

, .

, , , " , ".

class PeriodicObserver {
    bool dirty;
    public void notification(...) {
        // save the changed value; do nothing more.  Speed matters.
        this.dirty= True;
    }
    public result getMyValue() {
        if( this.dirty ) { 
            // recompute now
        }
        return the value
}
+4

push push-. , , , , :

class notifier { 
public:
    virtual void operator()() = 0;
};

class pull_notifier : public notifier { 
    bool dirty;
public:
    lazy_notifier() : dirty(false) {}
    void operator()() { dirty = true; }
    operator bool() { return dirty; }
};

class push_notifier : public notifier { 
    void (*callback)();
public:
    push_notifier(void (*c)()) : callback(c) {}
    void operator()() { callback(); }
};

push_notifier, pull_notifier , :

class MyDataClass { 
    notifier &notify;
    double m_member;
public:
    MyDataClass(notifier &n) : n_(n) {}
    void SetMember(double d) { 
        m_member = d; 
        notify();
    }
};

, - , . push_ pull_-. , pull_notifier push_notifiers, (), (, push_notifier, a pull_notifier).

+2

(push vs pull/polling). , , .

0

Source: https://habr.com/ru/post/1752709/


All Articles