ece 448/528 application software design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfece...

34
ECE 448/528 – Application Software Design Lecture 10 Event-Driven Programming and The Observer Pattern Professor Jia Wang Department of Electrical and Computer Engineering Illinois Institute of Technology February 17, 2020 ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 1/15

Upload: others

Post on 19-Mar-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

ECE 448/528 – Application Software DesignLecture 10 Event-Driven Programming

and The Observer Pattern

Professor Jia WangDepartment of Electrical and Computer Engineering

Illinois Institute of Technology

February 17, 2020

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 1/15

Page 2: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Outline

Event-Driven Programming

The Observer Pattern

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 2/15

Page 3: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Outline

Event-Driven Programming

The Observer Pattern

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 3/15

Page 4: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Applications

I Events: actions from various sources.I E.g. from external environment like user inputs and network

communications.

I An event-driven application runs forever, waiting for events tohappen and then reacting on them.I Cause more events to happen, either to be processed by the

application itself or to be sent to the external environment.

I Our IoT simulator is event-driven.I Timer events cause plugs to measure their power

consumptions.I HTTP requests may cause plugs to change its state. Plugs

further react by sending back their current state via HTTPresponses.

I The browser is event-driven.I User inputs cause the browser to send HTTP requests.I HTTP responses cause the browser to update its display.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 4/15

Page 5: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Applications

I Events: actions from various sources.I E.g. from external environment like user inputs and network

communications.

I An event-driven application runs forever, waiting for events tohappen and then reacting on them.I Cause more events to happen, either to be processed by the

application itself or to be sent to the external environment.

I Our IoT simulator is event-driven.I Timer events cause plugs to measure their power

consumptions.I HTTP requests may cause plugs to change its state. Plugs

further react by sending back their current state via HTTPresponses.

I The browser is event-driven.I User inputs cause the browser to send HTTP requests.I HTTP responses cause the browser to update its display.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 4/15

Page 6: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Applications

I Events: actions from various sources.I E.g. from external environment like user inputs and network

communications.

I An event-driven application runs forever, waiting for events tohappen and then reacting on them.I Cause more events to happen, either to be processed by the

application itself or to be sent to the external environment.

I Our IoT simulator is event-driven.I Timer events cause plugs to measure their power

consumptions.I HTTP requests may cause plugs to change its state. Plugs

further react by sending back their current state via HTTPresponses.

I The browser is event-driven.I User inputs cause the browser to send HTTP requests.I HTTP responses cause the browser to update its display.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 4/15

Page 7: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Applications

I Events: actions from various sources.I E.g. from external environment like user inputs and network

communications.

I An event-driven application runs forever, waiting for events tohappen and then reacting on them.I Cause more events to happen, either to be processed by the

application itself or to be sent to the external environment.

I Our IoT simulator is event-driven.I Timer events cause plugs to measure their power

consumptions.I HTTP requests may cause plugs to change its state. Plugs

further react by sending back their current state via HTTPresponses.

I The browser is event-driven.I User inputs cause the browser to send HTTP requests.I HTTP responses cause the browser to update its display.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 4/15

Page 8: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Programming

I A programming paradigm for event-driven applications.I General architecture: a dead loop of

I Waiting for an event to happen, e.g. a timer, a user input, or amessage from the network.

I Allow application to react to the event, via IoC or callbacks.I Allow application to trigger more events and to send those

events to their destinations.

I Usually, we rely on an event-driven framework or language totake care of the loop and the events.I So the developer only need to focus on event handling.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 5/15

Page 9: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Programming

I A programming paradigm for event-driven applications.I General architecture: a dead loop of

I Waiting for an event to happen, e.g. a timer, a user input, or amessage from the network.

I Allow application to react to the event, via IoC or callbacks.I Allow application to trigger more events and to send those

events to their destinations.

I Usually, we rely on an event-driven framework or language totake care of the loop and the events.I So the developer only need to focus on event handling.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 5/15

Page 10: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event-Driven Programming

I A programming paradigm for event-driven applications.I General architecture: a dead loop of

I Waiting for an event to happen, e.g. a timer, a user input, or amessage from the network.

I Allow application to react to the event, via IoC or callbacks.I Allow application to trigger more events and to send those

events to their destinations.

I Usually, we rely on an event-driven framework or language totake care of the loop and the events.I So the developer only need to focus on event handling.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 5/15

Page 11: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Challenges in Event Handling

I For event handling, developers need to specifyI What events are of interests?I How to process an event?I Where newly created events should be sent to?

I These are three separated responsibilities.I Don’t overlook the complexity when there are more than a few

objects producing and consuming events.I What class design should we have to support them?

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 6/15

Page 12: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Challenges in Event Handling

I For event handling, developers need to specifyI What events are of interests?I How to process an event?I Where newly created events should be sent to?

I These are three separated responsibilities.I Don’t overlook the complexity when there are more than a few

objects producing and consuming events.I What class design should we have to support them?

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 6/15

Page 13: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event Processing and Threading

I It is quite appealing to accelerate event processing by usingmultiple threads.

I However, as we usually need to update objects during eventprocessing, accessing the same object from multiple threadswithout protection may result in racing conditions.

I Method 1: use locks, e.g. synchronized methods, to protectobjects.I This is used by our Java code.

I Method 2: rely on thread confinement provided by theframework or the language, which guarantees events deliveredto the same object are processed in the same thread.I This is how JavaScript works.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 7/15

Page 14: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event Processing and Threading

I It is quite appealing to accelerate event processing by usingmultiple threads.

I However, as we usually need to update objects during eventprocessing, accessing the same object from multiple threadswithout protection may result in racing conditions.

I Method 1: use locks, e.g. synchronized methods, to protectobjects.I This is used by our Java code.

I Method 2: rely on thread confinement provided by theframework or the language, which guarantees events deliveredto the same object are processed in the same thread.I This is how JavaScript works.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 7/15

Page 15: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event Processing and Threading

I It is quite appealing to accelerate event processing by usingmultiple threads.

I However, as we usually need to update objects during eventprocessing, accessing the same object from multiple threadswithout protection may result in racing conditions.

I Method 1: use locks, e.g. synchronized methods, to protectobjects.I This is used by our Java code.

I Method 2: rely on thread confinement provided by theframework or the language, which guarantees events deliveredto the same object are processed in the same thread.I This is how JavaScript works.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 7/15

Page 16: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Event Processing and Threading

I It is quite appealing to accelerate event processing by usingmultiple threads.

I However, as we usually need to update objects during eventprocessing, accessing the same object from multiple threadswithout protection may result in racing conditions.

I Method 1: use locks, e.g. synchronized methods, to protectobjects.I This is used by our Java code.

I Method 2: rely on thread confinement provided by theframework or the language, which guarantees events deliveredto the same object are processed in the same thread.I This is how JavaScript works.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 7/15

Page 17: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Outline

Event-Driven Programming

The Observer Pattern

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 8/15

Page 18: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Design Patterns

I Common OOD/OOP practices to solve software designproblems

I Learn design experiences from expertsI Design patterns are solutions that are applied routinely.I Design patterns are independent of programming languages.

I Enable effective communication between designersI Design patterns are technical jargons for designers.I Allow you to quickly understand how a big piece of software is

organized.I Allow others to quickly comprehend your design idea.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15

Page 19: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Design Patterns

I Common OOD/OOP practices to solve software designproblems

I Learn design experiences from expertsI Design patterns are solutions that are applied routinely.I Design patterns are independent of programming languages.

I Enable effective communication between designersI Design patterns are technical jargons for designers.I Allow you to quickly understand how a big piece of software is

organized.I Allow others to quickly comprehend your design idea.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15

Page 20: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Design Patterns

I Common OOD/OOP practices to solve software designproblems

I Learn design experiences from expertsI Design patterns are solutions that are applied routinely.I Design patterns are independent of programming languages.

I Enable effective communication between designersI Design patterns are technical jargons for designers.I Allow you to quickly understand how a big piece of software is

organized.I Allow others to quickly comprehend your design idea.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15

Page 21: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Pattern

I A design pattern to decompose event processing and eventdelivery.

I EventSource: the class that produces eventsI As a consequence of processing events.

I Observer: an interface providing abstraction of event delivery.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 10/15

Page 22: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Pattern

I A design pattern to decompose event processing and eventdelivery.

I EventSource: the class that produces eventsI As a consequence of processing events.

I Observer: an interface providing abstraction of event delivery.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 10/15

Page 23: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Pattern

I A design pattern to decompose event processing and eventdelivery.

I EventSource: the class that produces eventsI As a consequence of processing events.

I Observer: an interface providing abstraction of event delivery.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 10/15

Page 24: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Design Problem

public class PlugSim {

...

synchronized public void switchOn() {

on = true;

}

...

}

I How should we update PlugSim so that we may notify othersthat the switch is on?I Indeed, if this is an actual smart plug, we may need to set the

GPIO connecting to the relay to 1.I We may also need to notify others across the network.

I So PlugSim is the EventSource in the observer pattern.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 11/15

Page 25: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Design Problem

public class PlugSim {

...

synchronized public void switchOn() {

on = true;

}

...

}

I How should we update PlugSim so that we may notify othersthat the switch is on?I Indeed, if this is an actual smart plug, we may need to set the

GPIO connecting to the relay to 1.I We may also need to notify others across the network.

I So PlugSim is the EventSource in the observer pattern.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 11/15

Page 26: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Interface

public class PlugSim {

...

public static interface Observer {

void update(String name, String key, String value);

}

...

}

I As the EventSource decide what events are produced, theObserver interface is defined as an inner/nested interface.

I A static inner interface depends on the PlugSim class butnot any PlugSim object.

I The update method indicates an event.I name: name of the plug.I key/value: details of the event, e.g. "state"/"on", or

"power"/"100".

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 12/15

Page 27: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Interface

public class PlugSim {

...

public static interface Observer {

void update(String name, String key, String value);

}

...

}

I As the EventSource decide what events are produced, theObserver interface is defined as an inner/nested interface.

I A static inner interface depends on the PlugSim class butnot any PlugSim object.

I The update method indicates an event.I name: name of the plug.I key/value: details of the event, e.g. "state"/"on", or

"power"/"100".

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 12/15

Page 28: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

The Observer Interface

public class PlugSim {

...

public static interface Observer {

void update(String name, String key, String value);

}

...

}

I As the EventSource decide what events are produced, theObserver interface is defined as an inner/nested interface.

I A static inner interface depends on the PlugSim class butnot any PlugSim object.

I The update method indicates an event.I name: name of the plug.I key/value: details of the event, e.g. "state"/"on", or

"power"/"100".

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 12/15

Page 29: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Observers Management

public class PlugSim {

...

private final ArrayList<Observer> observers = new ArrayList<>();

synchronized public void addObserver(Observer observer) {

observers.add(observer);

observer.update(name, "state", on? "on": "off");

observer.update(name, "power", String.format("%.3f", power));

}

...

}

I There could be multiple observers, the EventSource shoulduse a container to manage them.

I Usually an addObserver method adds an observer.I The EventSource may send events now so the observer could

initialize itself without waiting.I Be careful with multi-threading.

I But how does an Observer process an event?

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 13/15

Page 30: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Observers Management

public class PlugSim {

...

private final ArrayList<Observer> observers = new ArrayList<>();

synchronized public void addObserver(Observer observer) {

observers.add(observer);

observer.update(name, "state", on? "on": "off");

observer.update(name, "power", String.format("%.3f", power));

}

...

}

I There could be multiple observers, the EventSource shoulduse a container to manage them.

I Usually an addObserver method adds an observer.I The EventSource may send events now so the observer could

initialize itself without waiting.I Be careful with multi-threading.

I But how does an Observer process an event?

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 13/15

Page 31: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Observers Management

public class PlugSim {

...

private final ArrayList<Observer> observers = new ArrayList<>();

synchronized public void addObserver(Observer observer) {

observers.add(observer);

observer.update(name, "state", on? "on": "off");

observer.update(name, "power", String.format("%.3f", power));

}

...

}

I There could be multiple observers, the EventSource shoulduse a container to manage them.

I Usually an addObserver method adds an observer.I The EventSource may send events now so the observer could

initialize itself without waiting.I Be careful with multi-threading.

I But how does an Observer process an event?

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 13/15

Page 32: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Inversion of Control

public class PlugSim {

...

synchronized public void switchOn() {

updateState(true);

}

protected void updateState(boolean o) {

on = o;

logger.info("Plug {}: state {}", name, on? "on": "off");

for (Observer observer: observers) {

observer.update(name, "state", on? "on": "off");

}

}

...

}

I This is IoC.I We don’t care how an Observer processes an event.I Need to make sure the observers are notified whenever

necessary – via the updateState method.I protected methods cannot be accessed out of this and

dervied classes.I We don’t need a lock as long as public methods are locked.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 14/15

Page 33: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Inversion of Control

public class PlugSim {

...

synchronized public void switchOn() {

updateState(true);

}

protected void updateState(boolean o) {

on = o;

logger.info("Plug {}: state {}", name, on? "on": "off");

for (Observer observer: observers) {

observer.update(name, "state", on? "on": "off");

}

}

...

}

I This is IoC.I We don’t care how an Observer processes an event.I Need to make sure the observers are notified whenever

necessary – via the updateState method.I protected methods cannot be accessed out of this and

dervied classes.I We don’t need a lock as long as public methods are locked.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 14/15

Page 34: ECE 448/528 Application Software Design ... - ece.iit.edujwang/ece448-2020s/ece448-lec10.pdfECE 448/528 { Application Software Design, Spring 2020, Dept. of ECE, IIT 9/15. The Observer

Summary

I Event-driven programming.

I Use the observer pattern to separate event delivery from eventprocessing.

ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 15/15