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
34
Embed
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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
Outline
Event-Driven Programming
The Observer Pattern
ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 2/15
Outline
Event-Driven Programming
The Observer Pattern
ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 3/15
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
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
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
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
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
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
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
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
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
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
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
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
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
Outline
Event-Driven Programming
The Observer Pattern
ECE 448/528 – Application Software Design, Spring 2020, Dept. of ECE, IIT 8/15
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
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
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
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
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
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
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
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