Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer
Chair of Software Engineering ATOT - Lecture 20, 11 June Lecture 20: Agents and event-driven design
Chair of Software Engineering ATOT - Lecture 20, 11 June Agenda for today Scope of this development Applications The mechanism
Chair of Software Engineering ATOT - Lecture 20, 11 June Dogmatism Processor ObjectAction
Chair of Software Engineering ATOT - Lecture 20, 11 June Agenda for today Scope of this development Applications The mechanism
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June Compare to… ... “Functional” style of programming, e.g. Haskell Conjecture: Haskell should be an Eiffel library (Eifskell?)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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.”
Chair of Software Engineering ATOT - Lecture 20, 11 June The starting idea of object-technology Organize software architecture around data types. Agents: Can an object represent an action? Processor ObjectAction
Chair of Software Engineering ATOT - Lecture 20, 11 June Agenda for today Scope of this development Applications The mechanism
Chair of Software Engineering ATOT - Lecture 20, 11 June Applications of agents Iteration High-level contracts Numerical programming Introspection High-level functionals, type-safe
Chair of Software Engineering ATOT - Lecture 20, 11 June Integration example (1) b my_function (x) dx a my_integrator.integral (agent my_function, a, b)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June Agenda for today Scope of this development Applications The mechanism
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June Using a routine from another class agent some_object.some_routine (?, u, v) Target
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June Agents’ potential for contracts Express general properties such as “none of the elements from positions 1 to count – 1 have been changed”.
Chair of Software Engineering ATOT - Lecture 20, 11 June Event-driven programming PUBLISHERSSUBSCRIBERS trigger eventshandle events EVENTS ROUTINE
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June EiffelVision style my_button.click.action_list.extend (agent my_procedure)
Chair of Software Engineering ATOT - Lecture 20, 11 June Observer pattern (C++, Java) SUBSCRIBER * PUBLISHER * APPCLASS LIBCLASS attach detach update* update+ Deferred (abstract) Effective (implemented) * + Inherits from Client (uses)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June 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)
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June 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.
Chair of Software Engineering ATOT - Lecture 20, 11 June 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
Chair of Software Engineering ATOT - Lecture 20, 11 June Observer pattern (C++, Java) SUBSCRIBER * PUBLISHER * APPCLASS LIBCLASS attach detach update* update+ Deferred (abstract) Effective (implemented) * + Inherits from Client (uses)
Chair of Software Engineering ATOT - Lecture 20, 11 June End of lecture 20