UML类图
代码
import java. util. *;
interface state_interface {
public void check ( ) ;
public void updateValue ( float v) ;
public void setThreshold ( float threshold) ;
}
abstract class state implements state_interface {
public Threshold thre;
public float value;
state ( float threshold)
{
thre= new Threshold ( threshold) ;
}
@Override
public void setThreshold ( float v)
{
thre. SetThreshold ( v) ;
}
public void updateValue ( float v) {
value= v;
}
abstract public void check ( ) ;
}
class Threshold
{
private float max;
public Threshold ( float max)
{
this . max = max;
}
public void SetThreshold ( float max)
{
this . max = max;
}
public float Max ( )
{
return this . max;
}
}
class Temperature extends state
{
Temperature ( float v)
{
super ( v) ;
}
@Override
public void check ( ) {
if ( super . value > super . thre. Max ( ) )
System. out. println ( "temperature is out of range!" ) ;
}
}
class ph_value extends state
{
ph_value ( float v)
{
super ( v) ;
}
@Override
public void check ( ) {
if ( super . value > super . thre. Max ( ) )
System. out. println ( "PH is out of range!" ) ;
}
}
class pressure extends state
{
pressure ( float v)
{
super ( v) ;
}
@Override
public void check ( ) {
if ( super . value > super . thre. Max ( ) )
System. out. println ( "pressure is out of range!" ) ;
}
}
public class Alert
{
private ArrayList< state> stateArray;
public Alert ( )
{
stateArray = new ArrayList < state> ( ) ;
}
public void Add ( state s)
{
stateArray. add ( s) ;
}
public void Check ( )
{
for ( state s: stateArray)
{
s. check ( ) ;
}
}
}
运行结果