Developing Java Beans exposed externally. If Celsius is

Developing Java Beans exposed externally. If Celsius is not used, then Fahrenheit is being used, and vice versa. We’ll put off implementing the property until later. So the code for the Thermometer class now looks like this: package BeansBook.Simulator; public class Thermometer implements TempChangeListener { // a reference to the temperature object that we are monitoringprotected Temperature theTemperature; Thermometer(Temperature temperature) { theTemperature = temperature; // register for temperature change events theTemperature.addTempChangeListener(this); } // handle the temperature change events public void tempChanged(TempChangedEvent evt) { // do something with the temperature that we can retrieve // by calling evt.getTemperature() } // the get method for the DisplayingCelsius propertypublic boolean isDisplayingCelsius() { … } // an alternate get method for the DisplayingCelsius propertypublic boolean getDisplayingCelsius() { return isDisplayingCelsius(); } // the set method for the DisplayingCelsius propertypublic void setDisplayingCelsius(boolean value) { … } } In this example I’ve provided both forms of the get method for the boolean property DisplayingCelsius. You’ll notice that the only method that has an implementation so far is getDisplayingCelsius(), which does nothing but call the isDisplayingCelsius() method. We’ll fill in the code later. Whenever two methods are provided that perform the same function, it is a good idea to implement one in terms of the other. There is no requirement to do this, but it is a good programming practice. Following this guideline will eliminate the need to repeat code, and to modify multiple areas of code when the implementation changes. Normally when a property value is changed, the object will react in some way. Later, when our Thermometer object is capable of displaying a temperature value, we will have to implement code that reacts to a change to the DisplayingCelsius property by redisplaying the temperature according to the temperature units being used (Celsius or Fahrenheit). page 52

Comments are closed.