GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Feb-2007, 17:41
msm1593 msm1593 is offline
Junior Member
 
Join Date: Jan 2007
Posts: 32
msm1593 is on a distinguished road

Observable patterns


Hey 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  
Old 01-Mar-2007, 10:02
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Observable patterns


Quote:
Originally Posted by msm1593
Hey 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

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 GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:12.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.