The Springfield Nuclear Power Plant (2.0)

Image result for images homer simpson nuclear reactorDemo

A nuclear power plant consists of a reactor, a control panel, and various sensors—alarms, thermostats, thermometers, etc.

Here's a screenshot of the control panel. Clicking the INC TEMP button increases the temperature by 300 degrees. Clicking the DEC TEMP button decreases the temperature by 300 degrees.

When the reactor changes temperature, the thermometer in the center of the panel is automatically updated.

When the reactor's temperature is within 300 degrees of its critical temperature (7000 degrees) an alarm is triggered. The alarm displays a dialog box:

Attempting to increment the temperature above 7000 degrees triggers a thermostat which prevents the change:

Design

Our design follows the layered architecture pattern:

Notes

·       The presentation layer contains the control panel.

·       The reactor layer contains the reactor and sensors.

·       The utilities layer contains the Bean class, an event notification mechanism.

·       All layers depend on the Java layer.

The Reactor Layer

Notes

·       Reactors are beans that fire change events when their temperatures change. Sensors listen for these events and react accordingly when the occur:

o   Alarm's display a dialog if the temperature approaches the reactor's critical temperature.

o   A thermometer is a tiny window (JPanel) that contains a text field. When the temperature changes it is displayed in the text field.

o   A thermostat throws an exception when the temperature goes beyond the critical temperature. This prevents (vetoes) the change from occurring.

The Presentation Layer

Notes

·       The reactor panel contains the increment and decrement buttons. Buttons fire action events when they are clicked. The reactor panel listens for these events (ActionEventListener) and either increments or decrements the reactor's temperature by a fixed amount (= DELTA).

·       The reactor panel contains a thermometer panel between the buttons. Note that panels can contain controls (buttons, text fields, etc.) and sub-panels (Composite Pattern!).

·       The reactor creates three sensors and signs them up to listen for temperature change events from the reactor.

·       A reactor panel is the top-level control panel (JPanel). It's display method inserts it into a frame (JFrame), then sets the frame's visibility to true. This makes the frame (and the panel) appear on the desktop.

Implementation

Reactors and Sensors (reactor package)

Source: Reactor.java

Notes

·       Beans are objects that fire events when their properties change. Other objects—change event listeners-- can subscribe to a bean. They will be notified of any events fired by the bean. This is formalized in Bean.java.

·       A reactor is a bean that fires events when its temperature changes. This can only happen when the reactor's setTemperature method is called.

·       Before the change the reactor fires a vetoable change event. The thermostat listens for these events and throws an exception when one occurs. This prevents the temperature change from occurring.

·       If the change occurs, then the reactor fires a property change event. The alarm and thermometer listen for these events.

The User Interface (presentation package)

Source: ReactorPanel.java

Notes

·       After creating a reactor, the constructor creates sensors listening to the reactor.

·       The constructor creates the buttons then signs up to be notified when they're clicked (ActionEventListener).

·       The controls are added to the panel in the order they will appear. The panel uses a layout strategy to decide where to place the controls. The strategy can easily be changed (the Strategy Pattern!).

·       When a button is clicked, the actionPerformed method is automatically called. It identifies the command, then increments or decrements the reactor's temperature.

·       If the thermostat throws a veto exception, a message is displayed and the change doesn't happen.