title: How to handle Observables with different state-value types in the Observer
body: <p><em>(Context and question first, skeleton code at the bottom of the post)</em></p>
  
  <p>We are creating and implementing a C++ framework to use in environments like Arduino.</p>
  
  <p>For this I want to use the Observer pattern, where any component interested in state-changes of sensors (<code>Observables</code>) can register itself and it will get notified of those changes by the Observable calling the <code>notification()</code> method of the Observer with itself as a parameter.</p>
  
  <p>One Observer can observe multiple Observables, and vice versa.</p>
  
  <p>The problem lies in the fact that the Observer needs to extract the current state of the Observable and do something with it, and this current state can take all forms and sizes, depending on the particular sensor that is the Observable.</p>
  
  <p>It can of course be ordinal values, which are finite and can be coded out, like I did in the code below with the method <code>getValueasInt()</code> but it can also be sensor-specific structures, i.e. for a RealTimeClock, which delivers a struct of date and time values. The struct are of course defined at compile time, and fixed for a specific sensor. </p>
  
  <p>My question: <strong>What is the most elegant, and future-modification proof solution or pattern for this ?</strong></p>
  
  <p>Edit: Note that dynamic_cast&lt;> constructions are not possible because of Arduino limitations </p>
  
  <hr>
  
  <p>I have created the following class-hierarchy (skeleton code):</p>
  
  <pre><code>class SenseNode
  {
  public:
    SenseNode() {};
    SenseNode(uint8_t aNodeId): id(aNodeId) {}
    virtual ~SenseNode() {}
  
    uint8_t getId() { return id; };
  private:
    uint8_t id = 0;
  };
  
  class SenseStateNode : virtual public SenseNode
  {
  public:
    SenseStateNode(uint8_t aNodeId) : SenseNode(aNodeId) {}
    virtual ~SenseStateNode() {}
  
    /** Return current node state interpreted as an integer. */
    virtual int getValueAsInt();
  };
  
  class SenseObservable: public SenseStateNode
  {
  public:
    SenseObservable(uint8_t aNodeId);
    virtual ~SenseObservable();
    /** Notify all interested observers of the change in state by calling Observer.notification(this) */
    virtual void notifyObservers();
  protected:
    virtual void registerObserver(SenseObserver *);
    virtual void unregisterObserver(SenseObserver *);
  };
  
  class SenseObserver: virtual public SenseNode
  {
  public:
    SenseObserver() {};
    virtual ~SenseObserver();
  
    /** Called by an Observable that we are observing to inform us of a change in state */
    virtual void notification(SenseObservable *observable) {
      int v = observable-&gt;getValueAsInt(); // works like a charm
      DateTime d = observable-&gt; ????  // How should i solve this elegantly?
    };
  };
  </code></pre>
  
  <hr>