Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Event-driven programming.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
2 nd Microsoft Rotor Workshop, Pisa, April 23-25, SCOOPLI for.NET: a library for concurrent object-oriented programming Volkan Arslan, Piotr Nienaltowski.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Event-Driven programming, agents & tuples.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Chair of Software Engineering ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Design Patterns CS is not simply about programming
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Event-Driven programming, agents & tuples.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 18: Undo/Redo.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering 1 Introduction to Programming Tuples and Agents Exercise Session 1 and 2 December 2008.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC.
K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 15: Event-driven programming.
Object-Oriented Analysis and Design
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
C++ fundamentals.
Chair of Software Engineering JMLC 2003, , Klagenfurt Austria Event Library: an object-oriented library for event-driven design Volkan Arslan,
A First Program Using C#
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Model View.
An Introduction to Software Architecture
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
CS 326 Programming Languages, Concepts and Implementation
Observer Design Pattern
Chapter Eleven Handling Events.
Hands-on Introduction to Visual Basic .NET
Advanced Program Design with C++
An Introduction to Software Architecture
Advanced ProgramMING Practices
Advanced ProgramMING Practices
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Event-driven programming and Agents (Two-part lecture, second half next week)

2 Our goal for this (double) lecture We will extend our control structures with a more flexible mechanism where control is decentralized Resulting mechanism: agents (Eiffel); other languages have delegates (C#), closures (functional languages) Applications include:  Interactive, graphical programming (GUI) (Our basic example)  Iteration  Numerical programming  Concurrency Other

3 Handling input through traditional techniques Program drives user: from i := 0 read_line until end_of_file loop i := i + 1 Result [i ] := last_line read_line end

4 Handling input with modern GUIs User drives program: “When a user presses this button, execute that action from my program”

5 Event-driven programming: an example Specify that when a user clicks this button the system must execute find_station (x, y) where x and y are the mouse coordinates and find_station is a specific procedure of your system. CLICK START STATION ABOVE

6 Some issues 1. Keeping the “business model” and the UI separate  Business model (or just model ): core functionality of the application  UI: interaction with users 2. Minimizing “glue code” between the two 3. Preserving the ability to reason about programs and predict their behavior

7 Event-driven programming: a metaphor Routine Routine Routine Routine Routine Routine Routine Publishers Subscribers

8 Observing a value 35.4 Observers Subject

9 VIEW Observing a value A = 50% B = 30% C = 20% Subject

10 Alternative terminologies Observed / Observer Subject / Observer Publisher / Subscriber In this presentation: Publisher and Subscriber

11 Model-View Controller (Trygve Reenskaug, 1979)

12 Our example Specify that when a user clicks this button the system must execute find_station (x, y) where x and y are the mouse coordinates and find_station is a specific procedure of your system. CLICK START STATION ABOVE

13 Confusion Events Overview (from.NET documentation) Events have the following properties: 1. The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event. 2. An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers. 3. Events that have no subscribers are never called. 4. Events are commonly used to signal user actions such as button clicks or menu selections in graphical user interfaces. 5. When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. To invoke events asynchronously, see [another section]. 6. Events can be used to synchronize threads. 7. In the.NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class. Event Event type Uncertain

14 Event arguments Some events are characterized just by the fact of their occurrence Others have arguments:  A mouse click happens at position [x, y ]  A key press has a certain character code (if we have a single “key press” event type: we could also have a separate event type for each key)

15 An architectural solution: the Observer Pattern PUBLISHER * PUB_1 SUBSCRIBER * SUB_1 update * update + Deferred (abstract) Effective (implemented) * + Inherits from Client (uses) subscribe + unsubscribe + subscribed: LIST […] attach detach + + … PUB_2 … publish +

16 Design patterns A design pattern is an architectural scheme — a certain organization of classes and features — that provides applications with a standardized solution to a common problem Since 1994, various books have catalogued important patterns. Best known is Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Addison- Wesley 1994

17 A solution: the Observer Pattern PUBLISHER * PUB_1 SUBSCRIBER * SUB_1 update * update + Deferred (abstract) Effective (implemented) * + Inherits from Client (uses) subscribe + unsubscribe + subscribed: LIST […] attach detach + + … PUB_2 … publish +

18 Observer pattern Publisher keeps a (secret) list of observers: subscribed : LINKED_LIST [SUBSCRIBER] To register itself, an observer executes subscribe (some_publisher) where subscribe is defined in SUBSCRIBER : subscribe (p: PUBLISHER) -- Make current object observe p. require publisher_exists: p /= Void do p. attach (Current) end s1 s2s3s4 Polymorphic list

19 Attaching an observer In class PUBLISHER : feature {SUBSCRIBER} attach (s : SUBSCRIBER) -- Register s as subscriber to this publisher. require subscriber_exists : s /= Void do subscribed. extend (s ) end Note that the invariant of PUBLISHER includes the clause subscribed /= Void (List subscribed is created by creation procedures of PUBLISHER) Why?

20 publish -- Ask all observers to -- react to current event. do from subscribed. start until subscribed. after loop subscribed. item. subscribed. forthend Each descendant of SUBSCRIBER defines its own version of update Triggering an event update PUBLISHER * PUB_1 attach detach SUBSCRIBER * SUB_1 update * update + subscribed: LIST […] Dynamic binding sub Cursor item forth after s1 s2s3s4 subscribed publish +

21 Observer pattern (in basic form)  Publisher objects know about subscribers  Subscriber classes (and objects) know about their publishers  A subscriber may subscribe to at most one publisher  It may subscribe at most one operation  Handling of arguments (not detailed in previous slides) requires special care  The solution is not reusable: it must be coded anew for each application

22 Another approach: event-context-action table Set of triples [Event type, Context, Action] Event type: any kind of event we track Example: left mouse click Context: object for which these events are interesting Example: a particular button Action: what we want to do when an event occurs in the context Example: save the file Event-context-action table may be implemented as e.g. a hash table

23 Event-action table More precisely: Event_type – Action Table Left_click Save_file Event typeAction Reset More precisely: Event_type - Context – Action Table Right_click Display_Menu … … Save_button Context … … Left_click Cancel_button …Left_click Map Find_station

24 Action-event table Set of triples [Event, Context, Action] Event: any occurrence we track Example: a left click Context: object for which the event is interesting Example: the map widget Action: what we want to do when the event occurs in context Example: find the station closest to coordinates Action-event table may have various implementations, e.g. hash table. CLICK START STATION ABOVE

25 Mechanisms in other languages C and C++: “function pointers” C#: delegates (more limited form of agents)

26 Language note In non-O-O languages, e.g. C and Matlab, there is no notion of agent, but you can pass a routine as argument to another routine, as in integral (& f, a, b) where f is the function to integrate. & f (C notation, one among many possible ones) is a way to refer to the function f. (We need some such syntax because just `f ’ could be a function call.) Agents (or delegates in C#) provide a higher-level, more abstract and safer technique by wrapping the routine into an object with all the associated properties.

27 Event: each event type will be an object Example: left click Context: an object, usually representing a user interface element Example: the map Action: an agent representing a routine Example: find_station Using the Eiffel Event Library

28 The Event library Basically:  One generic class: EVENT_TYPE  Two features: publish and subscribe For example: A map widget Paris_map that reacts in a way defined in find_station when clicked (event left_click):

29 Example using the Event library The publisher (“subject”) creates an event type object: left_click : EVENT_TYPE [TUPLE [INTEGER, INTEGER]] -- Left mouse click events. once create Result ensure exists: Result /= Void end The publisher triggers the event: left_click. publish ([x_positition, y_position]) The subscribers (“observers”) subscribe to events: Paris_map. left_click. subscribe (agent find_station)

30 Event Library style The basic class is EVENT_TYPE On the publisher side, e.g. GUI library:  (Once) declare event type: click : EVENT_TYPE [TUPLE [INTEGER, INTEGER]]  (Once) create event type object: create click  To trigger one occurrence of the event: click. publish ([x_coordinate, y_coordinate]) On the subscriber side, e.g. an application: click. subscribe (agent find_station)

31 Observer pattern vs. Event Library In case of an existing class MY_CLASS :  With the Observer pattern: Need to write a descendant of SUBSCRIBER and MY_CLASS May lead to useless multiplication of classes Effect update to call appropriate model routine  With the Event Library: No new classes (use library classes directly) Can reuse the existing model routines directly as agents

32 A solution: the Observer Pattern PUBLISHER * PUB_1 SUBSCRIBER * SUB_1 update * update + Deferred (abstract) Effective (implemented) * + Inherits from Client (uses) subscribe + unsubscribe + subscribed: LIST […] attach detach + + … PUB_2 … publish +

33 click. subscribe (agent find_station) Paris_map. click. subscribe (agent find_station) click. subscribe (agent your_procedure (a, ?, ?, b) ) click. subscribe (agent other_object. other_procedure ) Subscriber variants

34 A word about tuples Tuple types (for any types A, B, C,... ): TUPLE TUPLE [A ] TUPLE [A, B ] TUPLE [A, B, C ]... A tuple of type TUPLE [A, B, C ] is a sequence of at least three values: first of type A, second of type B, third of type C Tuple values: e.g. [a1, b1, c1, d1 ]

35 Tuple type inheritance TUPLE [A, B ] TUPLE [A ] TUPLE

36 Labeled tuple types TUPLE [author : STRING ; year : INTEGER ; title : STRING] A labeled tuple type denotes the same type as the unlabeled form, here TUPLE [STRING, INTEGER, STRING] but facilitates access to individual elements:  To access tuple elements: e.g. t. year  To modify tuple elements: t. year := 1866 Labeled tuples amount to a restricted form of (anonymous) class. Exercise: write the class equivalent for the above. Example tuple: [”Tolstoi ”, 1865, ”War and Peace”]

37 What you can do with an agent a Call the associated routine through the feature call, whose argument is a single tuple: a. call ( [horizontal_position, vertical_position] ) If a is associated with a function, a. item ([...,...]) gives the result of applying the function. A manifest tuple

38 Tuples: Procedures vs. Functions Features applicable to an agent a:  If a represents a procedure, a. call ([argument_tuple]) calls the procedure  If a represents a function, a. item ([argument_tuple]) calls the function and returns its result

39 Using the Eiffel Event Library The basic class is TRAFFIC_EVENT_CHANNEL On the publisher side, e.g. GUI library:  (Once) declare event type: click: TRAFFIC_EVENT_CHANNEL [TUPLE [INTEGER, INTEGER]]  (Once) create event type object: create click  To trigger one occurrence of the event: click publish ([x_coordinate, y_coordinate]) On the subscriber side, e.g. an application: click subscribe (agent find_station)

40 What you can do with an agent a Call the associated routine through the feature call, whose argument is a single tuple: a. call ( [horizontal_position, vertical_position] ) If a is associated with a function, a. item ([...,...]) gives the result of applying the function. A manifest tuple

41 Keeping arguments open An agent can have both “closed” and “open” arguments Closed arguments set at time of agent definition; open arguments set at time of each call. To keep an argument open, just replace it by a question mark: u := agent a0. f (a1, a2, a3 ) -- All closed (as before) w := agent a0. f (a1, a2, ? ) x := agent a0. f (a1, ?, a3 ) y := agent a0. f (a1, ?, ? ) z := agent a0. f ( ?, ?, ? )

42 Calling the agent f (x1 : T1 ; x2 : T2 ; x3 : T3) a0 : C ; a1 : T1 ; a2 : T2 ; a3 : T3 u := agent a0. f (a1, a2, a3) v := agent a0. f (a1, a2, ?) w := agent a0. f (a1, ?, a3) x := agent a0. f (a1, ?, ?) y := agent a0. f (?, ?, ?) u. call ([]) v. call ([a3]) w. call ([a2]) x. call ([a2, a3]) y. call ([a1, a2, a3])

43 Another example of using agents my_integrator. integral ( agent my_function, a, b) my_integrator. integral (agent your_function ( ?, u, v ), a, b) a b  my_function (x ) dx b  your_function (x, u, v ) dx a

44 The integration function integral ( : FUNCTION [ANY, TUPLE [REAL], REAL]; a, b : REAL): REAL -- Integral of f over interval [a, b]. local x : REAL; i : INTEGER do from x := a until x > b loop Result := Result + f. item ([x])  step i := i + 1 x := a + i  step end f ab

45 Another application: using an iterator class C feature all_positive, all_married: BOOLEAN is_positive (n : INTEGER) : BOOLEAN -- Is n greater than zero? do Result := (n > 0) end intlist : LIST [INTEGER] emplist : LIST [EMPLOYEE] r do all_positive := intlist. for_all (agent is_positive (?) ) all_married := emplist. for_all (agent {EMPLOYEE}. is_married ) end class EMPLOYEE feature is_married : BOOLEAN … end

46 Iterators In class LINEAR [G ], ancestor to all classes for lists, sequences etc., you will find: for_all there_exists do_all do_if do_while do_until

47 Applications of agents Patterns: Observer, Visitor, Undo-redo (command) Iteration High-level contracts Numerical programming Introspection (finding out properties of the program itself)

48 Kernel library classes representing agents call last_result item * ROUTINE PROCEDURE + FUNCTION + PREDICATE +

49 Declaring an agent p: PROCEDURE [ANY, TUPLE] -- Agent representing a procedure, -- No open arguments. q: PROCEDURE [ANY, TUPLE [X, Y, Z]] -- Agent representing a procedure, -- 3 open arguments. f: FUNCTION [ANY, TUPLE [X, Y, Z], RES ] -- Agent representing a function, -- 3 open arguments, result of type RES.

50 Calling the agent f (x1 : T1 ; x2 : T2 ; x3 : T3) a0 : C ; a1 : T1 ; a2 : T2 ; a3 : T3 u := agent a0. f (a1, a2, a3) v := agent a0. f (a1, a2, ?) w := agent a0. f (a1, ?, a3) x := agent a0. f (a1, ?, ?) y := agent a0. f (?, ?, ?) u. call ([]) v. call ([a3]) w. call ([a2]) x. call ([a2, a3]) y. call ([a1, a2, a3])

51 Type of an agent f (x1 : T1 ; x2 : T2 ; x3 : T3) a0 : C ; a1 : T1 ; a2 : T2 ; a3 : T3 u := agent a0. f (a1, a2, a3) v := agent a0. f (a1, a2, ?) w := agent a0. f (a1, ?, a3) x := agent a0. f (a1, ?, ?) y := agent a0. f (?, ?, ?)

52 What we have seen The event-driven mode of programming, also known as publish-subscribe The Observer pattern Agents (closures, delegates…): encapsulating pure behavior in objects Applications to numerical programming and iteration