Hydraulic System Simulation - Extension
Modify the hydraulic simulation system developed in the previous assignment.
All the classes must belong to the package hydraulic
.
Overview
While in the previous assignment the simulation consisted in computing the flows and printing them
directly on the standard output, in this version we need to change this mechanism.
When an element is simulate, after computing the flows, instead of printing the results it must
notify another object (the observer) of its status.
Such a mechanism can be implemented by means of the pair of class Observable
and interface Observer
that are defined in the package java.util
and implement the
Observer pattern.
Class Observable manages:
- the registration of the observers interested in status change updates from the observable
(several observers are possible but often only one observer is registered),
by means of method
addObserver()
- sending the notification of the status change to the observer(s) together with additional information concerning the status (event object).
Interface Observer defines the method update() trough which observers receive the notification, such method accepts two arguments:
one is the Observable object that originated the notification and the other is the object containing the additional information (the event object)
Sending a notification from an observable element involves two steps:
- record the fact the the status of the Observable has changed, by means of method
setChanged()
,
- send the actual notification while providing the additional information (the event object), by means of method
notifyObservers()
.
With respect to the initial code provided in the previous assignment, the additional code added in this assignment is reported in
red bold monospaced font in order to make it easier
to reuse the code already developed for the previous assignment.
R1. Observable elements
Elements are observable items, and therefore the class Element extends Observable.
When the element is added to the system the system's observer must be added to the
element's observers list by means of the method addObserver()
defined in class Observer.
The system's observer is provided as an argument of the HSystem class constructor:
public HSystem(Observer obs){ }
R2. Simulation
When the simulation is performed, each element signals its updated values through
the method defined in class Observer. In particular, when computation of input and output flows is
complete:
- invokes on itself the method
setChanged()
and then
- invokes on itself the method
notifyObservers()
.
The method notifyObservers()
must be provided with an argument of class HEvent that
contains the information about the simulation as public attributes: the Element it refers to,
the input flow, and an array of output flows.
R3. Observer
The hydraulic system simulation can be monitored by an object of class HSObserver
that implements the interface java.util.Observer
.
In particular the method update() accepts as arguments an Element object (the Observable argument)
and an HEvent object (the Object argument). The method must print on the standard output, the name of the
element, the input flow and the output flow(s).
The following class diagram shows how classes in the hydaulic package are related to the Observer-Observable framework in java.util:
The following collaboration diagram shows how objects interact to carry on the notification: