Observer pattern - transmitting information to an observer

please excuse everyone and everyone newbieness)

I have the following task:

I have several classes (call them A) that constantly collect information from different sources (database, Internet, etc.). They may contain potentially large amounts of information on a particular topic.

I also have a bunch of classes (call these B) that let me display a lot of graphs if they are provided with enough information (here the information can be as simple as one or more values).

Now I need to link these two things together so that I can change what information is displayed in what form with minimal changes. Now I am considering using the observer pattern here because it seems to be well suited to this problem. However, I am a bit stuck and would like some advice. I can clearly make classes A of the basic type "data source", which allows subscribers to notify subscribers of changes. I can also create B classes of type "observer" and let them subscribe to data sources and receive notification of changes.

The problem is that I do not want my observers to know any specific type of information that they display. For example, if I want a graph in my city on the Y axis and time on the X axis, and I have a 2D plot class (type B), then I want to avoid any A->GetTemperature ()calls. Likewise, in class A, I do not want to call NotifyOfTemperatureChange ()or something like that ...

One idea is to define a bunch of transfers or lines, such as "temperature", "time", "humidity", etc., and then tell the viewer that he should listen to (something like A->SetYAxis (B, "temperature")- here, m informing Class A that he should get one float value for its Y axis from data source B, channel “temperature”)

A B->subscribeTo (whateverIPassedIn). , B , . notifyOfChangesOnThisChannel ("temperature"). , , ... , ? - :

void B::subscriberChanged (int subscriberId, std::string channel)
{
    float value = datasource [subscriberId].GetCurrentValue (channel);
}

-

void B::subscriberChanged (int subscriberId, std::string channel, void *data)
{
   float value = *static_cast <float *> (data);
}

, , B ? float, , int double?

, : ? ?

+3
1

, . ( ), , , Fact, , . AxisDescription, , (, , / ..).

:

template<class FactType, class AxisType>
class B {
public:
    b(AxisType axis); // initialization requires a description of the axis

    // called when a new fact is available
    void notify(FactType fact); 

    // called when many fact should be reported
    template<class FactIterator>
    void notify(FactIterator begin, FactIterator end); 
};

FactType , float int, , , .

+3

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


All Articles