Search code examples
design-patternsobserver-pattern

Name to Observer that can change values?


I'm developing a class where I associate events to observers, but in this case the observers are able to use the parameters received in parameter to change the values inside the object.

Is Observer the correct name for those "observers"? I'm just looking for the correct name to put that (if there's already a pattern for this case it would be great too)

Edit: Here's an example

A is an observable object

B is the observer

A have a property represented by an int, and B function it's to guarantee that the property it's always above 50.

So when A fires the event to B will be calling something like:

void BObserverMethod(Action action, A a)
{
    if(a.property =< 50)
       a.property = 50;
}

The question is: what can I call B? Is it correct to call it Observer when he do more than observe?


Solution

  • The observer pattern really just addresses the decoupled way in which multiple Observers can be updated about changes in a Subject. There's nothing in the pattern that prevents an observer from responding to an update by setting some value in the subject.

    However, common sense dictates that the observer must be careful about modifying observable properties of the subject; it would be easy for two or more such observers to set off round after round of updates. If two observers with opposite intentions both modified the same property, you could easily find yourself in an infinite loop:

    void BObserverMethod(Action action, A a)
    {
        if(a.property =< 50)
           a.property = 50;
    }
    
    void CObserverMethod(Action action, A a)
    {
        if(a.property >= 50)
           a.property = 35;
    }