java coding oop david davenport computer eng. dept., bilkent university ankara - turkey. email:...

17
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: [email protected] Towards Event-driven programming & Interfaces

Upload: guadalupe-mon

Post on 14-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Java Coding OOP

David Davenport

Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.

email: [email protected]

Towards Event-driven programming & Interfaces

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 thenset on and tell heater to switch off

if on & reading <= lowerLimit thenset 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

A Central-heating system… Object Diagram

roomTemp

t heater

{Thermostat}{Heater}

statelowerLimit

upperLimit

on

h

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

elseroomTemp--; // 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?

Generalise…{Thermostat}

{Heater}

{Heater}setState( false)

{AirConditioner}

{AirConditioner}setState( true)

{AlarmBell}

{AlarmBell}ring()

How can we have a

variable to hold any of

these things?

And have them all do

different things?

How…

{Thermostat}

handleAlarm

class AlarmListener Define as abstract parent class

{AlarmListener}(abstract)

{Heater}

setState

{AirConditioner}

setState

{AlarmBell}

ring

is_a is_ais_a

handleAlarm handleAlarm handleAlarm{AlarmListener}

handleAlarm

abstract void 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 thenset on and alarmListener.handleAlarm( false);

if on & reading <= lowerLimit thenset off and alarmListener.handleAlarm( true);

AlarmListener alarmListener;

public void addAlarmListener( AlarmListener listener)

Heater must be an AlarmListener

Heater class (extends AlarmListener)

boolean state

public Heater()

public boolean getState() public setState( state)

handleAlarm( boolean b)setState( b);

AlarmListener{abstract}

handleAlarm()

HeaterhandleAlarm()

is_a

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!

The Bell Hierarchy

Cannot “extend”

more than one

class!AlarmListener

{abstract}handleAlarm()

HeaterhandleAlarm()

is_a

Bell{abstract}

ring()

AlarmBellring()

DoorBellring()

is_ais_ais_a

Must add handleAlarm

The Bell Hierarchy

Bell{abstract}

ring()

AlarmBellring()

DoorBellring()

is_ais_a

{interface} AlarmListener

handleAlarm()

HeaterhandleAlarm()

implements

implements

Solution - make AlarmListener an Interface

Must add handleAlarm

Connecting Objects… What information is needed?

{Thermostat} {AlarmListener}

handleAlarm(-){AlarmListener}

{AlarmInfo}

sourcereading

nothingboolean

ObjectThermostat ” & reading

AlarmInfo

source