Software Architecture

Similar documents
Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Robotics Programming Laboratory

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Lecture 4: Observer Pattern, Event Library and Componentization

Design Patterns. An introduction

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Object-Oriented Software Construction

Design Patterns. Gunnar Gotshalks A4-1

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

DESIGN PATTERN - INTERVIEW QUESTIONS

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Introduction to Software Engineering: Object Design I Reuse & Patterns

Using Design Patterns in Java Application Development

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

A Reconnaissance on Design Patterns

WS01/02 - Design Pattern and Software Architecture

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

What is Design Patterns?

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Tuesday, October 4. Announcements

Concurrency Patterns in SCOOP

CS/CE 2336 Computer Science II

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

SDC Design patterns GoF

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

UNIT I Introduction to Design Patterns

A Metric of the Relative Abstraction Level of Software Patterns

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Object-oriented Software Design Patterns

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Object-Oriented Design

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Lecture 13: Design Patterns

Overview of Patterns: Introduction

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

The Strategy Pattern Design Principle: Design Principle: Design Principle:

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Topics in Object-Oriented Design Patterns

Software Engineering I (02161)

The GoF Design Patterns Reference

A Rapid Overview of UML

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

From Patterns to Components: The Factory Library Example

Software Design COSC 4353/6353 D R. R A J S I N G H

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Software Engineering I (02161)

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

3 Product Management Anti-Patterns by Thomas Schranz

What is Design Patterns?

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

INSTITUTE OF AERONAUTICAL ENGINEERING

A Metric for Measuring the Abstraction Level of Design Patterns

C++ for System Developers with Design Pattern

UNIT I Introduction to Design Patterns

CS251 Software Engineering Lectures 18: Intro to DP

An Introduction to Patterns

Object Oriented Paradigm

6.3 Patterns. Definition: Design Patterns

Applying the Observer Design Pattern

Ownership in Design Patterns. Master's Thesis Final Presentation Stefan Nägeli

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

1 Software Architecture

Object-Oriented Design

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

v. T_visit (Current) t.accept (v) CLIENT t v The Visitor pattern

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Introduction and History

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

DESIGN PATTERNS FOR MERE MORTALS

Design patterns generic models

Application Architectures, Design Patterns

Reuse at Design Level: Design Patterns

Design Patterns. CSC207 Fall 2017

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued)

Design Patterns. CSC207 Fall 2017

CHAPTER 6: CREATIONAL DESIGN PATTERNS

What is Design Patterns?

Summary of the course lectures

A Primer on Design Patterns

Design patterns. OOD Lecture 6

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

26.1 Introduction Programming Preliminaries... 2

Design Patterns: Structural and Behavioural

Transcription:

Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC 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. 2 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 3 1

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, http://se.inf.ethz.ch/people/arnout/patterns/ 4 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 5 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 6 2

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) 7 Pattern componentization Classification of design patterns: Fully componentizable Partially componentizable Wizard- or library-supported Non-componentizable Karine Arnout ETH PhD, 2004 Fully componentizable (48%) 8 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 236-271 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 23-30 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/ 9 3

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 10 Handling input through traditional techniques Program drives user: from End of input read_line count := 0 until exhausted loop count := count + 1 -- Store last_line at -- position count in Result Result [count ] := last_line read_line end 11 Handling input with modern GUIs User drives program: When a user presses this button, execute that action from my program 12 4

Multiple observers Observers or subscribers or subject, or publisher A = 50% B = 30% C = 20% Observed 13 Event-driven design Publishers Subscribers Routine Routine Routine Routine 14 Confusion Event Event type Uncertain 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. 15 5

Event-driven programming: example scenario One of your classes has a routine my_procedure Your application has a GUI object OK_button OK! Save file? Cancel 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 ) 16 Model-View Controller (Trygve Reenskaug, 1979) 17 MVC references Reenskaug s MVC page: heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html His original MVC paper: heim.ifi.uio.no/~trygver/1979/mvc-2/1979-12-mvc.pdf 18 6

Architecture: avoiding glue code Model View Controller (MVC) Design Pattern Event producer (e.g. GUI) Direct subscription Connection objects Model 19 A solution: the Observer Pattern * PUBLISHER attach detach subscribed: LIST [] * update* SUBSCRIBER subscribe + unsubscribe + (secret) PUB_3 SUB_4 SUB_3 PUB_2 + + SUB_2 PUB_1 SUB_1 update + * + Deferred (abstract) Effective (implemented) Inherits from Client (uses) 20 Observer pattern Each publisher keeps a list of subscribers: (secret) 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 21 7

Attaching an observer In class PUBLISHER : feature {SUBSCRIBER } attach (s : SUBSCRIBER ) (selective export) -- Register s as subscriber to current publisher. require subscriber_exists : s /= Void do subscribed.extend d (s ) s end sub1 sub2 sub n subscribers The invariant of PUBLISHER includes the clause subscribed /= Void (subscribed is created by creation procedures of PUBLISHER) 22 Triggering an event publish is -- Ask all observers to -- react to current -- event. do from subscribed.start until subscribed.after loop subscribed.item. subscribed.forth end end * PUBLISHER attach detach subscribed * SUBSCRIBER update* + + SUB_2 PUB_1 SUB_1 update + Dynamic binding! update subscribers update sub1 sub2 sub n Each descendant of SUBSCRIBER defines its own version of update 23 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 24 8

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. 25 Event-context-action table Event type Left_click Left_click Left_click Right_click Context OK_button Cancel_button Action Save_file Reset Display_Menu 26 Language mechanisms C and C++: function pointers C#: delegates Eiffel: agents 27 9

Example scenario (reminder) One of your classes has a routine my_procedure Your application has a GUI object known as OK_button OK! Save file? Cancel 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 ) 28 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 29 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) 30 10

Event-context-action table in.net Click deleg1 deleg2 deleg n OK_button Event type Context Action Left_click OK_button Save_file Left_click Cancel_button Reset Left_click Right_click Display_Menu 31 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 32 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 33 11

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) 34 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 Tuple type exists: Result /= Void end Tuple The publisher triggers the event: mouse_click.publish ([h, v ]) The subscribers ( observers ) subscribe to events: my_button.mouse_click.subscribe (agent my_procedure) 35 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) 36 12

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) 37 Architecture: avoiding glue code Model View Controller (MVC) Design Pattern Event producer (e.g. GUI) Direct subscription Connection objects Model 38 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 ) 39 13

In a concurrent context (SCOOP)) Use separate events: Volkan Arslan 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 40 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 h1 + h2 = λ i h1 (i) h2 (i) h \ t = λ i h (i-t) consequence: N x E Finite_history consequence (t, e) = exec (e) \ t s: subscribers (e) consequences: History History consequences (h) = λ i consequence (i) e: h (i) 41 Lessons Simplicity Search for the right abstractions Language matters 42 14