Chair of Software Engineering OOSC - Lecture 18 1 Object-Oriented Software Construction Bertrand Meyer.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

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.
1 Software Architecture Bertrand Meyer ETH Zurich, March-July 2009 Lecture 10: More patterns: Factory, Builder, Singleton.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
(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.
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.
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 Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC.
DESIGN PATTERNS Redesigning Applications And
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 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 ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 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.
Design Patterns.
Software Waterfall Life Cycle Requirements Construction Design Testing Delivery and Installation Operations and Maintenance Concept Exploration Prototype.
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.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
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.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Creational Patterns
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
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 و ابتغ فيما آتاك الله الدار.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Patterns in programming
Design Patterns: MORE Examples
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
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.
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Object Oriented Design Patterns - Creational Patterns
DESIGN PATTERNS : Introduction
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presented by Igor Ivković
Presentation transcript:

Chair of Software Engineering OOSC - Lecture 18 1 Object-Oriented Software Construction Bertrand Meyer

Chair of Software Engineering OOSC - Lecture 18 2 Lecture 18: From design patterns to components

Chair of Software Engineering OOSC - Lecture 18 3 Agenda for today  Design patterns  A successful story: the Observer pattern  From patterns to components

Chair of Software Engineering OOSC - Lecture 18 4 Agenda for today  Design patterns  A successful story: the Observer pattern  From patterns to components

Chair of Software Engineering OOSC - Lecture 18 5 Benefits of design patterns  Capture the knowledge of experienced developers  Publicly available “repository”  Newcomers can learn them and apply them to their design  Yield a better structure of the software (modularity, extendibility)  Common pattern language  Facilitate discussions between programmers and managers

Chair of Software Engineering OOSC - Lecture 18 6 However: 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.” Erich Gamma et al., Design Patterns, 1995 NOT REUSABLE

Chair of Software Engineering OOSC - Lecture 18 7 A step backwards  A step backwards from reuse:  No available “pattern libraries”  Programmers need to implement them each time anew  A pedagogical tool, not a reuse tool “A successful pattern cannot just be a book description: it must be a software component” Bertrand Meyer: OOSC2, 1997

Chair of Software Engineering OOSC - Lecture 18 8 Software reuse vs. design reuse  “Reuse of architectural and design experience is probably the single most valuable strategy in the basket of reuse ideas” Clemens Szyperski, Component software, 1998  Software reuse vs. design reuse:  Not much different with seamless development  Combining both worlds:  From patterns to Eiffel components…

Chair of Software Engineering OOSC - Lecture 18 9 Agenda for today  Design patterns  A successful story: the Observer pattern  From patterns to components

Chair of Software Engineering OOSC - Lecture A successful story: the Observer pattern OBSERVERSUBJECT ** MY_OBSERVERMY_SUBJECT update* update+ add_observer* remove_observer* notify_observers* add_observer+ remove_observer+ notify_observers+ * + Deferred (abstract) class Effective (concrete) class f* f+ Deferred feature Effective (implemented) feature inherits from client (uses)

Chair of Software Engineering OOSC - Lecture Class SUBJECT deferred class SUBJECT 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.has (an_observer) one_more: observers.count = old observers.count + 1 end 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

Chair of Software Engineering OOSC - Lecture Class SUBJECT (cont’d) 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 - Lecture Class OBSERVER deferred class OBSERVER feature -- Observer pattern update is -- Update observer according to the state of -- subject data. deferred end data: SUBJECT -- Observable data end

Chair of Software Engineering OOSC - Lecture A typical OBSERVER class MY_DISPLAY inherit OBSERVER redefine data end create make feature -- Initialization make is -- Initialize GUI and register an observer of data. do create add_button.make_with_text_and_action (“Add”, agent on_add) create remove_button.make_with_text_and_action (“Remove”, agent on_remove) data.add_observer (Current) end feature -- Access add_button: EV_BUTTON -- Button with label Add remove_button: EV_BUTTON -- Button with label Remove

Chair of Software Engineering OOSC - Lecture A typical OBSERVER (cont’d) data: MY_DATA -- Data to be observed feature -- Event handling on_add is -- Action performed when add_button is pressed do data.add end on_remove is -- Action performed when remove_button is pressed do data.remove end feature -- Observer pattern update is -- Update GUI. do -- Something here end

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

Chair of Software Engineering OOSC - Lecture 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 - Lecture An encouraging success  A book idea: the Observer pattern  A reusable library: the Event library Let’s go further and explore all design patterns…

Chair of Software Engineering OOSC - Lecture Agenda for today  Design patterns  A successful story: the Observer pattern  From patterns to components

Chair of Software Engineering OOSC - Lecture Objectives  A new classification of the design patterns described in Gamma et al.:  Artificial design patterns  Reusable design patterns  Remaining design patterns  A “pattern library” made of the reusable components obtained from design patterns  Code templates otherwise

Chair of Software Engineering OOSC - Lecture Creational design patterns Artificial design patterns Reusable design patterns Remaining design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Prototype: an artificial DP  Intent:  “Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.” [Gamma 1995, p 117] In Eiffel, every object is a prototype! CLIENT PROTOTYPE clone prototype Class Client relationship In fact: a feature of ANY

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Abstract Factory: a reusable DP  Intent:  “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.” [Gamma 1995, p 87]

Chair of Software Engineering OOSC - Lecture Class FACTORY deferred class FACTORY feature -- Factory methods new_product_a: PRODUCT_A is -- New product of type PRODUCT_A deferred ensure product_a_not_void: Result /= Void end new_product_b: PRODUCT_B is -- New product of type PRODUCT_B deferred ensure product_b_not_void: Result /= Void end

Chair of Software Engineering OOSC - Lecture Class FACTORY_1 class FACTORY_1 inherit FACTORY feature -- Factory methods new_product_a: PRODUCT_A1 is -- New product of type PRODUCT_A1 do create Result end new_product_b: PRODUCT_B1 is -- New product of type PRODUCT_B1 do create Result end

Chair of Software Engineering OOSC - Lecture Flaws of the approach  Code redundancy:  FACTORY_1 and FACTORY_2 will be similar  Lack of flexibility:  FACTORY fixes the set of factory functions new_product_a and new_product_b

Chair of Software Engineering OOSC - Lecture The Factory library class FACTORY [G] create make feature -- Initialization make (a_function: like factory_function) is -- Set factory_function to a_function. require a_function_not_void: a_function /= Void do factory_function := a_function ensure factory_function_set: factory_function = a_function end feature -- Access factory_function: FUNCTION [ANY, TUPLE [], G] -- Factory function creating new instances of type G

Chair of Software Engineering OOSC - Lecture The Factory library (cont’d) feature – Factory methods new: G is -- New instance of type G do factory_function.call ([]) Result := factory_function.last_result ensure new_not_void: Result /= Void end new_with_args (args: TUPLE): G is -- New instance of type G initialized with args do factory_function.call (args) Result := factory_function.last_result ensure new_not_void: Result /= Void end invariant factory_function_not_void: factory_function /= Void end

Chair of Software Engineering OOSC - Lecture Sample application simulated_traffic: TRAFFIC simulated_traffic.add_vehicle (…) VEHICLE * CAR + BUS + METRO + TRAFFIC + SIMULATION +

Chair of Software Engineering OOSC - Lecture With the Abstract Factory DP With: car_factory: CAR_FACTORY is -- Factory of cars once create Result ensure car_factory_not_void: Result /= Void end VEHICLE_FACTORY * CAR_FACTORY + BUS_FACTORY + METRO_FACTORY + new_car+new_metro+ new_vehicle* new_bus+ simulated_traffic.add_vehicle ( car_factory.new_car (a_power, a_wheel_diameter, a_door_width, a_door_height) )

Chair of Software Engineering OOSC - Lecture With the Factory library simulated_traffic.add_vehicle ( car_factory.new_with_args ([a_power, a_wheel_diameter, a_door_width, a_door_height] ) With: car_factory: FACTORY [CAR] is -- Factory of cars once create Result.make (agent new_car) ensure car_factory_not_void: Result /= Void end

Chair of Software Engineering OOSC - Lecture With the Factory library (cont’d) and: new_car (a_power,a_diameter,a_width,a_height: INTEGER):CAR is -- New car with power engine a_power, -- wheel diameter a_diameter, -- door width a_width, door height a_height do -- Create car engine, wheels, and doors. create Result.make (engine, wheels, doors) ensure car_not_void: Result /= Void end

Chair of Software Engineering OOSC - Lecture Factory pattern vs. library  Benefits:  Get rid of some code duplication  Fewer classes  Reusability  One caveat though:  Likely to yield a bigger client class (because similarities cannot be factorized through inheritance)

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Factory Method: a reusable DP  Intent:  “Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.” [Gamma 1995, p 107] A special case of the Abstract Factory

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Builder: a remaining DP  Intent:  “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” [Gamma 1995, p 97]

Chair of Software Engineering OOSC - Lecture Class BUILDER deferred class BUILDER feature -- Access last_product: PRODUCT is -- Product under construction deferred end feature -- Basic operations build is -- Create and build last_product. do build_product build_part_a build_part_b ensure last_product_not_void: last_product /= Void end... end

Chair of Software Engineering OOSC - Lecture A reusable builder?  Issue:  How to know how many parts the product has? Not reusable  Handle some usual cases, e.g. a “two part builder” by reusing the Factory library: class TWO_PART_BUILDER [F -> BUILDABLE, G, H] -- Build a product of type F -- composed of two parts: -- the first part of type G, -- the second part of type H.

Chair of Software Engineering OOSC - Lecture Class BUILDABLE deferred class BUILDABLE feature -- Access g: ANY -- First part of the product to be created h: ANY -- Second part of the product to be created feature {TWO_PART_BUILDER} -- Status setting -- set_g -- set_h end

Chair of Software Engineering OOSC - Lecture Creational design patterns  Prototype  Abstract Factory  Factory Method  Builder  Singleton

Chair of Software Engineering OOSC - Lecture Singleton: a remaining DP  Intent:  “Ensure a class only has one instance, and provide a global point of access to it.” [Gamma 1995, p 127] Harder than it looks… SHARED_ SINGLETON SINGLETON singleton

Chair of Software Engineering OOSC - Lecture A wrong approach class SINGLETON feature {NONE} -- Implementation frozen the_singleton: SINGLETON is -- The unique instance of this class once Result := Current end invariant only_one_instance: Current = the_singleton end

Chair of Software Engineering OOSC - Lecture A wrong approach (cont’d) deferred class SHARED_SINGLETON feature {NONE} -- Implementation singleton: SINGLETON is -- Access to unique instance deferred end is_real_singleton: BOOLEAN is -- Do multiple calls to singleton return the same result? do Result := singleton = singleton end invariant singleton_is_real_singleton: is_real_singleton end

Chair of Software Engineering OOSC - Lecture What’s wrong?  If one inherits from SINGLETON several times:  The inherited feature the_singleton keeps the value of the first created instance.  Violates the invariant of class SINGLETON in all descendant classes except the one for which the singleton was created first. There can only be one singleton per system

Chair of Software Engineering OOSC - Lecture A correct Singleton example class MY_SHARED_SINGLETON feature -- Access singleton: MY_SINGLETON is -- Singleton object do Result := singleton_cell.item if Result = Void then create Result.make end ensure singleton_created: singleton_created singleton_not_void: Result /= Void end feature -- Status report singleton_created: BOOLEAN is -- Has singleton already been created? do Result := singleton_cell.item /= Void end feature {NONE} -- Implementation singleton_cell: CELL [MY_SINGLETON] is -- Cell containing the singleton if already created once create Result.put (Void) ensure cell_not_void: Result /= Void end

Chair of Software Engineering OOSC - Lecture A correct Singleton example (cont’d) class MY_SINGLETON inherit MY_SHARED_SINGLETON create make feature {NONE} -- Initialization make is -- Create a singleton object. require singleton_not_created: not singleton_created do singleton_cell.put (Current) end invariant singleton_created: singleton_created singleton_pattern: Current = singleton end In fact, one can still break it by:  Cloning a singleton.  Using persistence.  Inheriting from MY_SHARED_SINGLETON and putting back Void to the cell after the singleton has been created.

Chair of Software Engineering OOSC - Lecture A Singleton in Eiffel: impossible?  Having frozen classes (from which one cannot inherit) would enable writing singletons in Eiffel  But it would still not be a reusable solution

Chair of Software Engineering OOSC - Lecture Structural design patterns Artificial design patterns Reusable design patterns Remaining design patterns  Composite  Flyweight  Proxy  Decorator  Adapter  Bridge  Facade

Chair of Software Engineering OOSC - Lecture Behavioral design patterns  Not done yet  But can expect DP like the Visitor and the Strategy to be reusable through the Eiffel agent mechanism

Chair of Software Engineering OOSC - Lecture References: Design patterns  Gamma et al.: Design Patterns: Elements of Reusable Object-Oriented Software, Addison- Wesley,  Jėzėquel et al.: Design Patterns and Contracts, Addison-Wesley, 1999.

Chair of Software Engineering OOSC - Lecture References: From patterns to components  Karine Arnout. Contracts and tests. Ph.D. research plan, December Available from  Karine Arnout, and Bertrand Meyer. “From Design Patterns to Reusable Components: The Factory Library”. Available from  Karine Arnout, and Éric Bezault. “How to get a Singleton in Eiffel?”. Available from  Volkan Arslan. Event library (sources). Available from  Volkan Arslan, Piotr Nienaltowski, and Karine Arnout. “Event library: an object-oriented library for event-driven design”. JMLC Available from MLC_2003_Arslan.pdf MLC_2003_Arslan.pdf  Bertrand Meyer. “The power of abstraction, reuse and simplicity: an object- oriented library for event-driven design”. Available from

Chair of Software Engineering OOSC - Lecture End of lecture 19