Note that there are some explanatory texts on larger screens.

plurals
  1. POobserver pattern -- passing information to the observer
    primarykey
    data
    text
    <p>please excuse any and all newbieness),</p> <p>I have the following task to perform:</p> <p>I have several classes (call these A) that constantly gather information from various sources (database, internet, etc). They can contain a potentially large amount of information on some topic..</p> <p>I also have a bunch of classes (call these B) that allow me to render a variety of graphs, if supplied with enough information (here, information can be as simple as one or more floating values).</p> <p>Now, I have to tie these two things together, so that I can modify which information is displayed in which form with minimal changes. Now, I'm considering using an observer pattern here, because it seems to lend itself nicely to this problem. However, I'm a bit stuck and would like advice. I can clearly make classes A of a "data source" base type, that allows subscribers and notifies subscribers when there are changes. I can also make classes B of the type "observer" and let them subscribe to data sources and be notified of changes. </p> <p>The problem is that I don't want my observers to know any particular type of information they are displaying. For instance, if I want to graph temperature in my city on the Y-axis and time on the X-axis, and I have a 2D-plot class (of type B), then I want to avoid any kind of <code>A-&gt;GetTemperature ()</code> calls. Similarly, in class A, I don't want to call <code>NotifyOfTemperatureChange ()</code> or anything like that...</p> <p>One idea is to define a bunch of enums or strings like <code>"temperature"</code>, <code>"time"</code>, <code>"humidity"</code>, etc and then tell the observer what it should listen to (something like <code>A-&gt;SetYAxis (B, "temperature")</code> -- here, I'm informing class A that it should get a single float value for its Y axis from data source B, channel "temperature")</p> <p>so that A can just do <code>B-&gt;subscribeTo (whateverIPassedIn)</code>. That way, B doesn't need to know what information its plotting. A can then say <code>notifyOfChangesOnThisChannel ("temperature")</code>. This, however, seems a bit hacky for me... For instance, would the data be actually passed to the notified function? Would I have to do something like this:</p> <pre><code>void B::subscriberChanged (int subscriberId, std::string channel) { float value = datasource [subscriberId].GetCurrentValue (channel); } </code></pre> <p>or would be it something like</p> <pre><code>void B::subscriberChanged (int subscriberId, std::string channel, void *data) { float value = *static_cast &lt;float *&gt; (data); } </code></pre> <p>and besides, how would B know the type of data? I mean in this example it's a float, but what if it's an int or a double?</p> <p>I guess my question is: is this correct? Is there a nicer way to approach this problem?</p> <p>Thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload