Chair of Software Engineering OOSC - Summer Semester 2005 1 Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
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.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming.
Design Patterns William A. Hoffman NYU OOP Class.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Chair of Software Engineering OOSC - Lecture 18 1 Object-Oriented Software Construction Bertrand Meyer.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns.
樣式導向設計 (Pattern-Oriented Design) 課程簡介 Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
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.
Powerpoint Templates Page 1 Powerpoint Templates What is Design Patterns ? by Indriati Teknik Informatika – UB.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
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.
Methods: Deciding What to Design In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr Fall 2005 ICE0575 Lecture.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Title Carolina First Steering Committee October 9, 2010 Online Voting System Design Yinpeng Li and Tian Cao May 3, 2011.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Creational Patterns
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
樣式導向設計 (Pattern-Oriented Design) 課程簡介
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
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
Presentation transcript:

Chair of Software Engineering OOSC - Summer Semester Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture 9: Introduction to Patterns, Model View Controller and Observer Pattern Till G. Bay

Chair of Software Engineering OOSC - Summer Semester Introduction to Patterns, Model View Controller and Observer Pattern  What is a Pattern?  Model View Controller Pattern  Observer Pattern  Event Library

Chair of Software Engineering OOSC - Summer Semester Design pattern: Gang of Four’s description “A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object- oriented design.” Erich Gamma et al., Design Patterns: Elements of Reusable Object-Oriented Software, 1995, p 3.

Chair of Software Engineering OOSC - Summer Semester Design pattern: A definition A design pattern is a set of domain-independent architectural ideas — typically a design scheme describing some classes involved and the collaboration between their instances — captured from real-world systems that programmers can learn and apply to their software in response to a specific problem. Karine Arnout, From Patterns to Components, 2004

Chair of Software Engineering OOSC - Summer Semester Description of a design pattern A design pattern is given by one or more of  A description of the pattern’s intent  Use cases  A software architecture for typical implementations

Chair of Software Engineering OOSC - Summer Semester GoF’s description of a design pattern  Pattern name and classification  Intent  Also known as  Motivation  Applicability  Structure  Participants  Collaborations  Consequences  Implementation  Sample code  Known uses  Related patterns Categorization by intent

Chair of Software Engineering OOSC - Summer Semester The GoF design patterns  Creational  Abstract Factory  Builder  Factory Method  Prototype  Singleton  Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy  Behavioral  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor

Chair of Software Engineering OOSC - Summer Semester Creational design patterns (1/2)  Creational  Abstract Factory  Builder  Factory Method  Prototype  Singleton  Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy  Behavioral  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor

Chair of Software Engineering OOSC - Summer Semester Creational design patterns (2/2)  Goal:  Put more flexibility into the instantiation process  How:  Through inheritance or delegation  What:  Defer parts of object creation

Chair of Software Engineering OOSC - Summer Semester Structural design patterns (1/2)  Creational  Abstract Factory  Builder  Factory Method  Prototype  Singleton  Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy  Behavioral  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor

Chair of Software Engineering OOSC - Summer Semester Structural design patterns (2/2)  Goal:  Compose software elements into bigger structures  How:  Through inheritance (static binding) or composition (flexibility)

Chair of Software Engineering OOSC - Summer Semester Behavioral design patterns (1/2)  Creational  Abstract Factory  Builder  Factory Method  Prototype  Singleton  Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy  Behavioral  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor

Chair of Software Engineering OOSC - Summer Semester Behavioral design patterns (2/2)  Deal with:  Algorithms  Assignment of responsibilities between objects  Communication between objects  How:  Through inheritance or composition

Chair of Software Engineering OOSC - Summer Semester VIEW Model View Controller A = 50% B = 30% C = 20% Views Model

Chair of Software Engineering OOSC - Summer Semester Model View Controller Model Encapsulates application state Exposes application functionality Notifies view of changes View Renders the model Sends user gestures to controller Allows controller to select view Controller Defines application behavior Maps user actions to model updates Selects view for response One for each functionality View selection User gestures State change Change Notification Events Feature calls

Chair of Software Engineering OOSC - Summer Semester VIEW Observer A = 50% B = 30% C = 20% Observer s Subject

Chair of Software Engineering OOSC - Summer Semester Observer pattern * OBSERVER * SUBJECT + MY_OBSERVER + MY_SUBJECT update* update+ add_observer* add_observer+ remove_observer* remove_observer+ notify_observers* notify_observers+ subject observers “Define[s] a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” [GoF, p 293]

Chair of Software Engineering OOSC - Summer Semester Class SUBJECT (1/2) deferred class SUBJECT inherit ANY redefine default_create end feature {NONE} -- Initialization default_create is -- Initialize observers. do create observers.make end feature -- Observer pattern add_observer (an_observer: OBSERVER) is -- Add an_observer to the list of observers. require not_yet_an_observer: not observers.has (an_observer) do observers.extend (an_observer) ensure observer_added: observers.last = an_observer one_more: observers.count = old observers.count + 1 end

Chair of Software Engineering OOSC - Summer Semester Class SUBJECT (2/2) remove_observer (an_observer: OBSERVER) is -- Remove an_observer from the list of observers. require is_an_observer: observers.has (an_observer) do observers.search (an_observer) observers.remove ensure observer_removed: not observers.has (an_observer) one_less: observers.count = old observers.count – 1 end notify_observers is -- Notify all observers. (Call update on each observer.) do from observers.start until observers.after loop observers.item.update observers.forth end observers: LINKED_LIST [OBSERVER] -- List of observers invariant observers_not_void: observers /= Void end

Chair of Software Engineering OOSC - Summer Semester Class OBSERVER deferred class OBSERVER feature -- Observer pattern update is -- Update observer according to the state of -- the subject it is subscribed to. deferred end

Chair of Software Engineering OOSC - Summer Semester Book library example (1/4) class LIBRARY inherit SUBJECT redefine default_create end feature {NONE} -- Initialization default_create is -- Create and initialize the library with an empty -- list of books. do Precursor {SUBJECT} create books.make end

Chair of Software Engineering OOSC - Summer Semester Book library example (2/4) feature -- Access books: LINKED_LIST [BOOKS] -- Books currently in the library feature -- Element change add_book (a_book: BOOK) is -- Add a_book to the list of books and notify all library observers. require a_book_not_void: a_book /= Void not_yet_in_library: not books.has (a_book) do books.extend (a_book) notify_observers ensure one_more: books.count = old books.count + 1 book_added: books.last = a_book end... invariant books_not_void: books /= Void no_void_book: not books.has (Void) end

Chair of Software Engineering OOSC - Summer Semester Book library example (3/4) class APPLICATION inherit OBSERVER rename update as display_book redefine default_create end feature {NONE} -- Initialization default_create is -- Initialize library and subscribe current application as -- library observer. do create library library.add_observer (Current) end …

Chair of Software Engineering OOSC - Summer Semester Book library example (4/4) feature -- Observer pattern library: LIBRARY -- Subject to observe display_book is -- Display title of last book added to library. do print (library.books.last.title) end invariant library_not_void: library /= Void consistent: library.observers.has (Current) end

Chair of Software Engineering OOSC - Summer Semester A typical SUBJECT Redundancy:  Hardly maintainable  Not reusable class MY_DATA inherit SUBJECT feature -- Observer pattern add is -- Add Current to data to be observed. do -- Do something. notify_observers end remove is -- Remove Current from data to be observed. do -- Do something. notify_observers end

Chair of Software Engineering OOSC - Summer Semester Drawbacks of the Observer  The subject knows its observers  No information passing from subject to observer when an event occurs  An observer can register to at most one subject  Could pass the SUBJECT as argument to update but would yield many assignment attempts to distinguish between the different SUBJECTs.

Chair of Software Engineering OOSC - Summer Semester 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):

Chair of Software Engineering OOSC - Summer Semester 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 mouse_click_not_void: Result /= Void end  The publisher triggers the event: mouse_click.publish ([x_positition, y_position])  The subscribers (“observers”) subscribe to events: my_button.mouse_click.subscribe (agent my_procedure)

Chair of Software Engineering OOSC - Summer Semester 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 OOSC - Summer Semester Publisher, subscriber, subscribed object (1/2)  Publisher: Responsible for triggering (“publishing”) events. (Corresponds to the subject of the Observer pattern.)  Subscribed object: Notified whenever an event (of the event type they are subscribed to) is published. (Corresponds to the observer of the Observer pattern.)  Subscriber: Registers subscribed objects to a given event type. (Corresponds to the class, which registers the observers to the subjects.)

Chair of Software Engineering OOSC - Summer Semester Publisher, subscriber, subscribed object (2/2) Subscriber (APPLICATION) Subscribed objectsPublishers Subscribes objects to events

Chair of Software Engineering OOSC - Summer Semester Book library example with the Event Library (1/2) class LIBRARY … feature -- Access books: LINKED_LIST [BOOK] -- Books in library feature -- Event type book_event: EVENT_TYPE [TUPLE [BOOK]] -- Event associated with attribute books

Chair of Software Engineering OOSC - Summer Semester Book library example with the Event Library (2/2) feature -- Element change add_book (a_book: BOOK) is -- Add a_book to the list of books and -- publish book_event. require a_book_not_void: a_book /= Void not_yet_in_library: not books.has (a_book) do books.extend (a_book) book_event.publish ([a_book]) ensure one_more: books.count = old books.count + 1 book_added: books.last = a_book end invariant books_not_void: books /= Void book_event_not_void: book_event /= Void end

Chair of Software Engineering OOSC - Summer Semester Observer pattern vs. Event Library  In case of an existing class MY_CLASS:  With the Observer pattern:  Need to write a descendant of OBSERVER and MY_CLASS  Useless multiplication of classes  With the Event Library:  Can reuse the existing routines directly as agents

Chair of Software Engineering OOSC - Summer Semester End of lecture 9