Module: Essential Tools Module Group: Miscellaneous Classes
Does not inherit
#include <rw/model.h> (abstract base class)
This abstract base class has been designed to implement the "Model" leg of a Model-View-Controller architecture. A companion class, RWModelClient, supplies the "View" leg.
It maintains a list of dependent RWModelClient objects. When member function changed(void*) is called, the list of dependents will be traversed, calling updateFrom(RWModel*, void*) for each one, with itself as the first argument. Subclasses of RWModelClient should be prepared to accept such a call.
None
This is an incomplete and somewhat contrived example in that it does not completely define the classes involved. "Dial" is assumed to be a graphical representation of the internal settings of "Thermostat." The essential point is that there is a dependency relationship between the "Thermostat" and the "Dial": when the setting of the thermostat is changed, the dial must be notified so that it can update itself to reflect the new setting of the thermostat.
#include <iostream> #include <rw/model.h> class Dial : public RWModelClient { int dialNumber; public: explicit Dial(int /* dialNumber */); virtual void updateFrom(RWModel* /* m */, void* /* d */); }; class Thermostat : public RWModel { double setting; public: Thermostat( Dial* d ) { addDependent (d); setting = 0; } double temperature() const { return setting; } void setTemperature(double t) { setting = t; changed (); } }; void Dial::updateFrom(RWModel* m, void*) { Thermostat* t = (Thermostat*)m; double temp = t -> temperature (); // Redraw graphic. std::cout << "Dial #" << dialNumber << " says " << temp << std::endl; } Dial::Dial (int num) : dialNumber (num) { } int main () { Dial one(1); Dial two(2); Thermostat therm(&one); therm.setTemperature(77); therm.setTemperature(-4); therm.addDependent(&two); therm.setTemperature(47); return 0; }
Program output:
Dial #1 says 77 Dial #1 says -4 Dial #1 says 47 Dial #2 says 47
RWModel();
When called by the specializing class, sets up the internal ordered list of dependents.
void addDependent(RWModelClient* m);
Adds the object pointed to by m to the list of dependents of self.
virtual void changed(void* d);
Traverse the internal list of dependents, calling member function updateFrom(RWModel*, void*) for each one, with self as the first argument and d as the second argument.
void removeDependent(RWModelClient* m);
Removes the object pointed to by m from the list of dependents of self.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.