View Single Post
  #3 (permalink)  
Old 04-01-2008, 05:15 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: Model View Controller

An Example Model--Temperature

Here is a model that is actually too simple to really be built as a class. It encapsulates the notion of a temperature in either Farenheit or Celsius units. When we create a temperature object it is at the freezing point of water but we can ask for its value in either type of unit and we can likewise set its value using either kind of unit. Notice that there are no GUI elements here, but there is some infrastructure, namely the setChanged and notifyObserver calls in the mutators.

public class TemperatureModel extends java.util.Observable
{ public double getF(){return temperatureF;}

public double getC(){return (temperatureF - 32.0) * 5.0 / 9.0;}

public void setF(double tempF)
{ temperatureF = tempF;
setChanged();
notifyObservers();
}

public void setC(double tempC)
{ temperatureF = tempC*9.0/5.0 + 32.0;
setChanged();
notifyObservers();
}

private double temperatureF = 32.0;
}



Not that it is especially interesting, but you can set a Farenheit temperature and then get the equivalent Celsius temperature. Thus this acts as a temperature converter as well as a temperature.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Reply With Quote