Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Instant Update


/*
 * TemperatureModel.java
 */

import java.util.Observable;

/** 
 * In Java, MVC Models can be easily implemented by extending the 
 * java.util.Observable class. The Java Observer/Observable classes provide much 
 * of needed infrastructure for passing registration/notification events back 
 * and forth between Model, View and Controller objects. Here the Observable 
 * superclass contains the methods needed to register observers and notify them 
 * of state changes to the Model.
 * 
 * 

This particular class models temperature. It allows allows clients (e.g., 
 * views and controllers) to query and update the data model both in terms of 
 * Fahrenheit and Celsius temperature scales. Upon construction, the model is 
 * initialized to a temperature of 32F (freezing point of water).
 */
public class TemperatureModel extends Observable {

	public static final int MAX_TEMP_F = 300;		// max allowable temp in the
model
	public static final int MIN_TEMP_F = -200;		// min allowable temp in the
model
	
    private double tempF = 32.0;
    
	/**
	 * Return the current temp, expressed in Fahrenheit. Method may be invoked 
	 * by View objects.
	 * 
	 * @return double temp in F
	 */
    public double getF() { return tempF; }
	
	/**
	 * Return the current temp, expressed in Celsius. Method may be invoked
	 * by View objects
	 * 
	 * @return double temp in C
	 */
    public double getC() { return (tempF-32.0)*5.0/9.0; }
    
	/**
	 * Modify the current temp, argument expressed in Fahrenheit. Method may 
	 * be invoked by Controller objects. Method implements some simple bounds 
	 * checking to make sure the new temp falls in the allowed range (between 
	 * MAX and MIN allowable temps).
	 */
    public void setF(double tempF) {
		// simple bounds checking - thermometer goes up to 300F and down to
-200F
		if ( tempF > MAX_TEMP_F ) tempF = MAX_TEMP_F;
		if ( tempF < MIN_TEMP_F ) tempF = MIN_TEMP_F;
		
        this.tempF = tempF;
        setChanged();       // <-- MVC method that signals state change in the
Observable object 
        notifyObservers();  // <-- MVC method that notifies all interested
observers of the state change
    }

	/**
	 * Modify the current temp, argument expressed in Celsius. Method may 
	 * be invoked by Controller objects. Method implements some simple bounds
	 * checking to make sure the new temp falls in the allowable range (between
	 * MAX and MIN allowable temps).
	 */
    public void setC(double tempC) {
		// simple bounds checking - thermometer goes up to 300F and down to
-200F
		double val = tempC * 9.0 / 5.0 + 32.0;
		if ( val > MAX_TEMP_F ) val = MAX_TEMP_F;
		if ( val < MIN_TEMP_F ) val = MIN_TEMP_F;
		
        this.tempF = val;
        setChanged();       // <-- MVC method that signals state change in the
Observable object 
        notifyObservers();  // <-- MVC method that notifies all interested
observers of the state change
    }
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.