Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 1 Advanced Topics in Object Technology Bertrand Meyer.

Similar presentations


Presentation on theme: "Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 1 Advanced Topics in Object Technology Bertrand Meyer."— Presentation transcript:

1 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 1 Advanced Topics in Object Technology Bertrand Meyer

2 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 2 Lecture 20: Agents and event-driven design

3 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 3 Agenda for today  Scope of this development  Applications  The mechanism

4 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 4 Dogmatism Processor ObjectAction

5 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 5 Agenda for today  Scope of this development  Applications  The mechanism

6 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 6 Scope  Starting from an object-oriented basis, add a new kind of objects representing potential computations.  Such objects are called “agents”.  Earlier names:  Delayed calls  Routine objects  Similar to:  “Closures”  Delegates (C#)  Blocks (Smalltalk)  Lambda expressions

7 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 7 Compare to… ... “Functional” style of programming, e.g. Haskell  Conjecture: Haskell should be an Eiffel library (Eifskell?)

8 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 8 Extensions (Eiffel: The Language, 1991)  “There is one and only one kind of acceptable language extension: the one that dawns on you with the sudden self-evidence of morning mist. It must provide a complete solution to a real problem, but usually that is not enough: almost all good extensions solve several potential problems at once, through a simple addition. It must be simple, elegant, explainable to any competent user of the language in a minute or two. It must fit perfectly within the spirit and letter of the rest of the language. It must not have any dark sides or raise any unanswerable questions. And because software engineering is engineering, you must see the implementation technique.”

9 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 9 The starting idea of object-technology  Organize software architecture around data types.  Agents: Can an object represent an action? Processor ObjectAction

10 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 10 Agenda for today  Scope of this development  Applications  The mechanism

11 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 11 Applications of agents  Iteration  High-level contracts  Numerical programming  Introspection  High-level functionals, type-safe

12 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 12 Integration example (1) b  my_function (x) dx a my_integrator.integral (agent my_function, a, b)

13 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 13 b  your_function (x, u, v) dx a my_integrator.integral (agent your_function (?, u, v), a, b)  In the first example (one argument), the notation agent my_function  is a synonym for agent my_function (?) Integration example (2)

14 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 14 Agenda for today  Scope of this development  Applications  The mechanism

15 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 15 Open and closed arguments agent your_function (?, u, v)  Closed: set at the time of the agent’s definition  Open: set at the time of any call to the agent Closed Open

16 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 16 Using a routine from another class agent some_object.some_routine (?, u, v) Target

17 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 17 Iteration  Consider my_integer_list: LIST [INTEGER]  in a class C that has the function is_positive (x: INTEGER): BOOLEAN is -- Is x positive? do Result := (x > 0) end  To test that all integers in a list are positive: all_positive := my_integer_list.for_all (agent is_positive)

18 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 18 Iteration (cont’d)  Consider my_employee_list: LIST [EMPLOYEE]  where class EMPLOYEE has the feature is_married: BOOLEAN -- Does this object represent a -- married employee?  To test that all employees in a list are married: all_married := my_employee_list.for_all (agent {EMPLOYEE}.is_married)

19 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 19 Target or argument open  Compare the two examples (both in a class C): my_integer_list: LIST [INTEGER] my_employee_list: LIST [EMPLOYEE] is_positive (x: INTEGER): BOOLEAN-- In class C is_married: BOOLEAN-- In class EMPLOYEE -- Abbreviated as -- my_integer_list.for_all (agent is_positive): my_integer_list.for_all (agent is_positive (?)) my_employee_list.for_all (agent {EMPLOYEE}.is_married) Open

20 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 20 An EiffelBase contract (class HASH_TABLE) extend (new: G; key: H) -- Assuming there is no item of key key, -- insert new with key; set inserted. require not_key_present: not has (key) ensure insertion_done: item (key) = new key_present: has (key) inserted: inserted one_more: count = old count + 1

21 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 21 Agents’ potential for contracts  Express general properties such as “none of the elements from positions 1 to count – 1 have been changed”.

22 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 22 Event-driven programming PUBLISHERSSUBSCRIBERS trigger eventshandle events EVENTS ROUTINE

23 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 23 Event Library  Class EVENT_TYPE  Publisher side, e.g. GUI library:  (Once) declare event type: click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]]  (Once) create event type object: create click  Each time the event occurs: click.publish ([x_coordinate, y_coordinate])  Subscriber side: click.subscribe (agent my_procedure)

24 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 24 Subscriber variants click.subscribe (agent my_procedure) my_button.click.subscribe (agent my_procedure) click.subscribe (agent your_procedure (a, ?, ?, b)) click.subscribe (agent other_object.other_procedure)

25 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 25 EiffelVision style my_button.click.action_list.extend (agent my_procedure)

26 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 26 Observer pattern (C++, Java) SUBSCRIBER * PUBLISHER * APPCLASS LIBCLASS attach detach update* update+ Deferred (abstract) Effective (implemented) * + Inherits from Client (uses)

27 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 27 Observer pattern  Publishers know about subscribers  Subscriber may subscribe to at most one publisher  May subscribe at most one operation  Not reusable — must be coded anew for each application

28 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 28 Event library  Publisher, e.g. GUI library:  Declare and create: click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]]  Trigger each event with arguments. click.publish ([x, y])  Subscriber (to subscribe a routine r): my_button.click.subscribe (agent r)

29 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 29.NET event-delegate mechanism  Publisher or subscriber:  Introduce descendant ClickArgs of EventArgs repeating types of arguments of myProcedure. (Adds a class.) public class ClickArgs { int x, y;... }  Declare delegate type ClickDelegate based on that class. (Adds a type.) public void delegate ClickDelegate (Object sender, ClickArgs e); D1 D2

30 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 30  Declare new event type Click based on the type ClickDelegate. (Adds a type.) public event ClickDelegate Click;  Write procedure OnClick to wrap handling. (Adds a routine.) protected void OnClick (ClickArgs e) { if (Click != null) Click (this, e); }  For every event occurrence, create instance of ClickArgs, passing arg values to constructor. (Adds a run-time object.) ClickArgs myClickArgs = new ClickArgs (h, v);  For every occurrence, trigger event OnClick (myClickArgs);.NET delegates (2): publisher D3 D4 D5 D6

31 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 31.NET delegates (3): subscriber  To subscribe a routine myProcedure:  Declare a delegate myDelegate of type ClickDelegate. (Can be combined with following step as shown next.)  Instantiate it with myProcedure as constructor’s argument. ClickDelegate myDelegate = new ClickDelegate (myProcedure)  Add it to the delegate list for the event. yourButton.Click += myDelegate D7 D8 D9

32 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 32.NET delegates (4)  event is a keyword of the language (special features of a class). But event types should be treated as ordinary objects.  Cannot have closed arguments: for equivalent of r (a, ?, ?, b) must write routine wrapper to be used for delegate.  Cannot have open target: for equivalent of {TYPE}.r (...) must write routine wrapper.

33 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 33 Lessons  Avoid magic: what’s available to the language designer should be available to the programmer  Role of language mechanisms: genericity, constrained genericity, tuples  Importance of choosing the right abstractions  Observer Pattern: PUBLISHER, SUBSCRIBER .NET: event, delegate, event type, delegate type?  Eiffel Event Library: EVENT_TYPE

34 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 34 Observer pattern (C++, Java) SUBSCRIBER * PUBLISHER * APPCLASS LIBCLASS attach detach update* update+ Deferred (abstract) Effective (implemented) * + Inherits from Client (uses)

35 Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 35 End of lecture 20


Download ppt "Chair of Software Engineering ATOT - Lecture 20, 11 June 2003 1 Advanced Topics in Object Technology Bertrand Meyer."

Similar presentations


Ads by Google