Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing Observer pattern when observers wish to observe different items
    primarykey
    data
    text
    <p>Below I have attempted to write a sudo code for the Observer pattern when observers wish to observe different items.</p> <p>Ignore the syntax errors. I wish to know if this is the correct way to implement this. If not, please suggest better ways.</p> <pre><code>// Used by the subject for keeping a track of what items the observer wants to observe typedef struct observerListStruct { bool getTemperatureUpdate; bool getHumidityUpdate; bool getPressureUpdate; observer's-function pointer's address; }; // Subject's class class weatherData { public: // Observers will call this function to register themselves. The function pointer will point to the function which will get called when updates are available. void registerObservers (observer obj, observer's-FunctionPointer) { // This observer's function returns which items to observe. char* f = obj.returnItemsToObserve (); if f[0] = `1` observerListStruct.getTemperatureUpdate = true; } void unregisterObservers (observer obj) {} private: vector &lt;observerListStruct&gt; observerList; float temperature; float humidity; float pressure; void notifyObservers () {} float getTemperature () {} float getHumidity () {} float getPressure () {} } weatherDataObject; // Base class for observers containing common functions class observers { char ItemsToObserve [3] = {1, 2, 3}; // This observer's function returns which items to observe. Default - return all items virtual char* returnItemsToObserve () { return ItemsToObserve; } }; class observerDisplayElementCurrentConditions : public observers { char ItemsToObserve [3] = {1, 2}; char* returnItemsToObserve () { return ItemsToObserve; } // this function will be used as a function pointer for getting updates void getUpdatesAndDisplayWeatherData (float, float) {} }; </code></pre>
    singulars
    1. This table or related slice is empty.
    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