170 likes | 282 Views
Java Coding OOP. Towards Event-driven programming & Interfaces. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…
E N D
Java Coding OOP Towards Event-driven programming & Interfaces David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr
IMPORTANT… • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! • Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you,David.
Central Heating control… • Would like to model a room with a thermostatically controlled heater • Need thermostat & heater objects • Thermostat must “tell” heater to switch off when room warm enoughand on again when too cold
Thermostat class • int lowerLimit, upperLimit • boolean on // true if >= upperLimit, false if <= lowerLimit • public Thermostat( int lower, int upper) • public boolean isOn() • public void update( int reading) if !on & reading >= upperLimit then set on and tell heater to switch off if on & reading <= lowerLimit then set off and tell heater to switch on
Heater class • boolean state • public Heater() • public boolean getState() • public setState( state)
Sending messages… • Thermostat “tells” Heater to switch on/off • In update method of Thermostat • public void update( int reading) {if ( !on && reading >= upperLimit) {on = true; h.setState( false);} else if ( on && reading <= lowerLimit) {on = false; h.setState( true);}} • Need to connect Heater to Thermostat • Heater h; // new property added • public void setHeater( Heater h) { this.h = h;} h is heater
lowerLimit state upperLimit on h A Central-heating system… • Object Diagram roomTemp t heater {Thermostat} {Heater} setState(…)
A Central-heating system… • Thermostat t = new Thermostat(18, 20); • Heater heater = new Heater(); • t.setHeater( heater); • int roomTemp = 0; • heater.setState( true); • for ( int time = 0; time < 100; time++) { if ( heater.getState() ) // if heater is on roomTemp++; // temp increases else roomTemp--; // else decreases! t.update( roomTemp); System.out.println( roomTemp);}
Generalise… • OK if only want Thermostats to switch Heaters on and off, • BUT would also like to use them to control AirConditioners, and to ring AlarmBells, etc! • HOW?
{Thermostat} {Heater} {Heater} setState( false) {AirConditioner} {AirConditioner} setState( true) {AlarmBell} {AlarmBell} ring() Generalise… How can we have a variable to hold any of these things? And have them all do different things?
{AlarmListener}(abstract) handleAlarm is_a is_a is_a setState setState ring handleAlarm {Heater} {AirConditioner} {AlarmBell} {Thermostat} How… class AlarmListener abstract void handleAlarm(…) Define as abstract parent class {AlarmListener} handleAlarm handleAlarm handleAlarm
Thermostat class • int lowerLimit, upperLimit • boolean on • public Thermostat( int lower, int upper) • public int isOn() • public void update( int reading) if !on & reading >= upperLimit then set on and alarmListener.handleAlarm( false); if on & reading <= lowerLimit then set off and alarmListener.handleAlarm( true); Heater must be an AlarmListener • AlarmListener alarmListener; • public void addAlarmListener( AlarmListener listener)
Heater class (extends AlarmListener) • boolean state • public Heater() • public boolean getState() • public setState( state) • handleAlarm( boolean b) setState( b); AlarmListener {abstract} handleAlarm() is_a HeaterhandleAlarm()
Boiling water alarm… • Would like to model a kettle which sounds an alarm bell when water boils • Need thermostat & alarm bell objects • Thermostat must “tell” alarm bell to ring when water boils • Need AlarmBell to extend AlarmListener BUT… AlarmBell already in Bell class hierarchy!
AlarmListener {abstract} handleAlarm() Bell {abstract} ring() is_a is_a is_a is_a AlarmBellring() DoorBell ring() HeaterhandleAlarm() The Bell Hierarchy Cannot “extend” more than one class! Must add handleAlarm
{interface}AlarmListener handleAlarm() Bell {abstract} ring() implements is_a is_a implements AlarmBellring() DoorBell ring() HeaterhandleAlarm() The Bell Hierarchy • Solution - make AlarmListener an Interface Must add handleAlarm