Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC.

Slides:



Advertisements
Similar presentations
Object Oriented Game Framework Design Riz Verghese Joj.
Advertisements

Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Event-Driven programming, agents & tuples.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Event-Driven programming, agents & tuples.
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
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 17: Event-driven programming.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
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 ATOT - Lecture 2, 2 April Advanced Topics in Object Technology Bertrand Meyer.
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.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Chair of Software Engineering JMLC 2003, , Klagenfurt Austria Event Library: an object-oriented library for event-driven design Volkan Arslan,
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
1 Software Architecture Bertrand Meyer ETH Zurich, March-July 2009 Lecture 11: More patterns: Bridge, Composite, Decorator, Façade, Flyweight.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Testing Extensible Design Patterns in OO Frameworks through Scenario Templates D.S. Sanders Software Verification & Validation.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns Introduction
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Software Engineering Lecture 7 - Design Patterns
Patterns.
DESIGN PATTERNS : Introduction
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presentation transcript:

Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC

2 Patterns in software development Design pattern:  A document that describes a general solution to a design problem that recurs in many applications. Developers adapt the pattern to their specific application.

3 Some design patterns Creational  Abstract Factory  Builder  Factory Method  Prototype  Singleton Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy Behavioral  Chain of Responsibility  Command (undo/redo)  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor

4 References Erich Gamma, Ralph Johnson, Richard Helms, John Vlissides: Design Patterns, Addison-Wesley, 1994 Jean-Marc Jezequel, Michel Train, Christine Mingins: Design Patterns and Contracts, Addison-Wesley, 1999 Karine Arnout: From Patterns to Components, 2004 ETH thesis,

5 Benefits of design patterns  Capture the knowledge of experienced developers  Publicly available repository  Common pattern language  Newcomers can learn & apply patterns  Yield better software structure  Facilitate discussions: programmers, managers

6 A pattern is not a reusable solution Solution to a particular recurring design issue in a particular context:  “Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to this problem in such a way that you can use this solution a million times over, without ever doing it the same way twice.” Gamma et al. NOT REUSABLE

7 A step backwards? Patterns are not reusable solutions:  You must implement every pattern every time  Pedagogical tools, not components We have done work at ETH to correct this situation: “A successful pattern cannot just be a book description: it must be a software component” Result: Pattern Library and Pattern Wizard (see following lectures)

8 Pattern componentization Classification of design patterns:  Fully componentizable  Partially componentizable  Wizard- or library-supported  Non-componentizable Karine Arnout ETH PhD, 2004 Fully componentizable (48%)

9 Pattern componentization: references Bertrand Meyer:The power of abstraction, reuse and simplicity: an object-oriented library for event-driven design, in From Object- Orientation to Formal Methods: Essays in Memory of Ole-Johan Dahl, Lecture Notes in Computer Science 2635, Springer-Verlag, 2004, pages se.ethz.ch/~meyer/ongoing/events.pdf se.ethz.ch/~meyer/ongoing/events.pdf Karine Arnout and Bertrand Meyer: Pattern Componentization: the Factory Example, in Innovations in Systems and Software Technology (a NASA Journal) (Springer-Verlag), 2006 se.ethz.ch/~meyer/publications/nasa/factory.pdf Bertrand Meyer and Karine Arnout: Componentization: the Visitor Example, in Computer (IEEE), vol. 39, no. 7, July 2006, pages se.ethz.ch/~meyer/publications/computer/visitor.pdf Karine Arnout’s thesis: From Patterns to Components, March 2004 se.inf.ethz.ch/people/arnout/patterns/

10 Our first set of patterns & componentization Observer pattern Model-View Controller Improving on Observer: a more general & flexible approach Implementing the solution in C#/.NET Implementing the solution in Eiffel

11 Handling input through traditional techniques Program drives user: from read_line count := 0 until exhausted loop count := count Store last_line at -- position count in Result Result [count ] := last_line read_line end End of input

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

13 Multiple observers A = 50% B = 30% C = 20% Observers Observed or subscribers or subject, or publisher

14 Routine Event-driven design Routine Routine Routine Routine Routine Routine Publishers Subscribers

15 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

16 Event-driven programming: example scenario One of your classes has a routine my_procedure Your application has a GUI object OK_button Whenever the user clicks the mouse the underlying GUI library returns the mouse coordinates You want to ensure that a mouse click at coordinates [h, v ] calls my_procedure (h, v ) OK! Cancel Save file?

17 Model-View Controller (Trygve Reenskaug, 1979)

18 MVC references Reenskaug’s MVC page: heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html His original MVC paper: heim.ifi.uio.no/~trygver/1979/mvc-2/ MVC.pdf

19 Architecture: avoiding glue code Model View Controller (MVC) Design Pattern Event producer (e.g. GUI) Model Connection objects Direct subscription

20 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 + + (secret) SUB_2 SUB_3 PUB_2 PUB_3 SUB_4

21 Observer pattern Each publisher keeps a list of subscribers: feature {NONE } subscribed : LINKED_LIST [SUBSCRIBER] To register itself, a subscriber may execute: subscribe (some_publisher ) where subscribe is defined in SUBSCRIBER as: subscribe (p: PUBLISHER ) is -- Make current object observe p. require publisher_exists : p /= Void do p. attach (Current ) end (secret)

22 Attaching an observer In class PUBLISHER : feature {SUBSCRIBER } attach (s : SUBSCRIBER ) -- Register s as subscriber to current publisher. require subscriber_exists : s /= Void do subscribed. extend (s ) end The invariant of PUBLISHER includes the clause subscribed /= Void (subscribed is created by creation procedures of PUBLISHER) (selective export) s sub1 subscribe rs sub2 sub n

23 Triggering an event publish is -- Ask all observers to -- react to current -- event. do from subscribed. start until subscribed. after loop subscribed. item. subscribed. forthend update PUBLISHER * PUB_1 SUBSCRIBER * SUB_1 update* update + subscribed attach detach + + SUB_2 subscribers sub1 sub2 sub n update Dynamic binding! Each descendant of SUBSCRIBER defines its own version of update

24 Observer pattern  Subscriber may subscribe to at most one publisher  May subscribe at most one operation  Publishers internally know about subscribers  Not reusable — must be coded anew for each application

25 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.

26 Event-context-action table Left_click Save_file Event typeAction Reset Right_click Display_Menu … … OK_button Context … … Left_click Cancel_button …

27 Language mechanisms C and C++: function pointers C#: delegates Eiffel: agents

28 Example scenario (reminder) One of your classes has a routine my_procedure Your application has a GUI object known as OK_button Whenever the user clicks the mouse the underlying GUI library returns the mouse coordinates You want to ensure that a mouse click at coordinates [h, v] calls my_procedure (h, v ) OK! Cancel Save file?

29 With.NET delegates: publisher (1) P1. Introduce new class ClickArgs inheriting from EventArgs, repeating arguments types of my_procedure: public class ClickArgs {... int x, y; … } P2. Introduce new type ClickDelegate (delegate type) based on that class public void delegate ClickDelegate (Object sender, e) P3. Declare new type Click (event type) based on the type ClickDelegate: public event ClickDelegate Click

30 With.NET delegates: publisher (2) P4. Write new procedure OnClick to wrap handling: protected void OnClick (ClickArgs ca) {if (Click != null) {Click (this, ca. x, ca. y);}} P5. To publish an event of the given type, create new object (instance of ClickArgs), passing arguments to constructor: ClickArgs myClickArgs = new ClickArgs (h, v); P6. To publish an event of the given type, trigger event: OnClick (myClickArgs)

31 Event-context-action table in.NET Left_click Save_file Event typeAction Reset Right_click Display_Menu … … OK_button Context … … Left_click Cancel_button … deleg1 deleg2 deleg n Click OK_button

32 With.NET delegates: subscriber D1. Declare a delegate myDelegate of type ClickDelegate. (Usually combined with following step.) D2. Instantiate it with my_procedure as argument: ClickDelegate = new ClickDelegate (my_procedure) D3. Add it to the delegate list for the event: OK_button.Click += myDelegate

33 Abstractions behind the Eiffel Event Library Event: each event type will be an object Example: mouse clicks Context: an object, usually representing element of user interface Example: a particular button Action: an agent representing a routine Example: routine to save the file

34 The Event library Basically:  One generic class: EVENT_TYPE  Two features: publish and subscribe For example: A button my_button that reacts in a way defined in my_procedure when clicked (event mouse_click)

35 Example using the Event library The publisher (“subject”) creates an event type object: mouse_click :EVENT_TYPE [TUPLE [INTEGER, INTEGER] ] is -- Mouse click event type once create Result ensure exists: Result /= Void end The publisher triggers the event: mouse_click. publish ( [h, v ] ) The subscribers (“observers”) subscribe to events: my_button. mouse_click. subscribe (agent my_procedure) Tuple Tuple type

36 Event Library specification 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 my_procedure )

37 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  Useless multiplication of classes  With the Event Library: Can reuse the existing routines directly as agents some_event_type. subscribe (agent existing_routine)

38 Architecture: avoiding glue code Model View Controller (MVC) Design Pattern Event producer (e.g. GUI) Model Connection objects Direct subscription

39 Subscriber variants click. subscribe (agent my_procedure) my_button. click.subscribe (agent my_procedure) click. subscribe (agent my_procedure(a,?,?,b)) click. subscribe (agent other_object. other_procedure )

40 In a concurrent context (SCOOP)) Use “separate” events: temperature_change : separate EVENT_TYPE [TUPLE [REAL]] temperature_change. subscribe (agent my_operation ) Library provides periodic events, synchronous events… See Volkan Arslan, Piotr Nienaltowski, and Karine Arnout. “Event library: an object-oriented library for event-driven design”. JMLC 2003  se.ethz.ch/people/arslan/data/scoop/conferences/Event_Libra ry_JMLC_2003_Arslan.pdf se.ethz.ch/people/arslan/data/scoop/conferences/Event_Libra ry_JMLC_2003_Arslan.pdf Volkan Arslan

41 Towards a theory of event-driven computation Execution is solution of h = root + consequences (h) Actor Event_type, abbreviated E Finite_history = P (E)* History = P (E) ∞ exec: Actor  Finite_history subscribers: E  P (Actor) root: Actor consequence: N x E  Finite_history consequence (t, e) = ∑ exec (e) \ t s: subscribers (e) h1 + h2 = i | h1 (i)  h2 (i) h \ t = i | h (i - t) consequences: History  History consequences (h) = i | ∑ consequence (i) e: h (i)

42 Lessons Simplicity Search for the right abstractions Language matters