![]() |
|
#1
|
|||
|
|||
Observable patternsHey guys, i was recently assigned some homework where i was to implement an observable pattern in an already running program. The program when given to me had a thermostat and a heater. The user could hit either "warmer" or "cooler" of the aplet. What i need to do is update the thermostat by the heater instance notifying its oberservers, not the event handlers for the buttons...
Now, i'm not going to paste the code in entirety, because i'm not really even sure where to start here, i was wondering is someone could post a very simple example of an observer pattern (or link to one) as i haven't even seen one before, and the ones i find online all seem to be rather complicated Also, after looking at the docs, i notice there is an addObserver() function, how could this be used to create an arrayList of observers? Thank you very much! Also, if anyone has any valuable knowledge in general about observer patterns it would be greatly appreciated, i've only been working with java for about 5 weeks now |
|||
|
#2
|
|||
|
|||
Re: Observable patternsQuote:
Technically, the event functions in Java are, by definition, following the Observer Pattern. ( Here's some general Observer stuff: http://en.wikipedia.org/wiki/Observer_pattern ) If you extend your "heater" class with Observable ( http://java.sun.com/j2se/1.5.0/docs/...bservable.html ) then you can implement the Observer interface ( http://java.sun.com/j2se/1.5.0/docs/.../Observer.html ) on the "thermostat" object. That way you can call Heater.addObserver( ThermostatInstance ). Then when a change is made, you can call Heater.notifyObservers(). I hope that gets you going in the right direction.... |
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The