Chair of Software Engineering OOSC - Summer Semester 2004 1 Object-Oriented Software Construction Bertrand Meyer.

Slides:



Advertisements
Similar presentations
Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Advertisements

SCOOP: Simple Concurrent Object-Oriented Programming Extend the pure, strongly typed, object-oriented language Eiffel with a general and powerful concurrency.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 6 Advanced Data Modeling.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 5 Advanced Data Modeling.
Database Systems: Design, Implementation, and Management Tenth Edition
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 6 Advanced Data Modeling.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Introduction To System Analysis and Design
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
Chair of Software Engineering ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 20: Some design principles.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
1 Software Architecture Bertrand Meyer ETH Zurich, March-May 2009 Lecture 14: Designing for reuse.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 12: Design by Contract™
Chair of Software Engineering OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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 4: The Interface of a Class.
Chair of Software Engineering ATOT - Lecture 9, 30 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 16, 26 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering ATOT - Lecture 11, 7 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chapter 1 Principles of Programming and Software Engineering.
Chair of Software Engineering ATOT - Lecture 15, 21 May Advanced Topics in Object Technology Bertrand Meyer.
1 Introduction to: Design by Contract Fall 2005 OOPD John Anthony.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Ranga Rodrigo. Class is central to object oriented programming.
Comparison of OO Programming Languages © Jason Voegele, 2003.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Introduction To System Analysis and Design
Chapter 8 Data Modeling Advanced Concepts Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Software Testing Reference: Software Engineering, Ian Sommerville, 6 th edition, Chapter 20.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Software Architecture
Presentation transcript:

Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer

Chair of Software Engineering OOSC - Summer Semester Lecture 14: Some design principles

Chair of Software Engineering OOSC - Summer Semester Simplicity  Command-query separation principle:  Clear, understandable interfaces  Systematic naming conventions  Operand-option separation:  Dramatically simplified feature interfaces

Chair of Software Engineering OOSC - Summer Semester The view from a traditional library (NAG) nonlinear_ode (equation_count: in INTEGER; epsilon: in out DOUBLE; func: procedure (eq_count: INTEGER; a: DOUBLE; eps: DOUBLE; b: ARRAY [DOUBLE]; cm: pointer Libtype); left_count, coupled_count: INTEGER …) [And so on. Altogether 19 arguments, including:  4 in out values;  3 arrays, used both as input and output;  6 functions, each with 6 or 7 arguments, of which 2 or 3 arrays!]

Chair of Software Engineering OOSC - Summer Semester The EiffelMath routine... Set up the non-default values... e.solve... Solve the problem, recording the answer in x and y...

Chair of Software Engineering OOSC - Summer Semester The Consistency Principle  All the components of a library should proceed from an overall coherent design, and follow a set of systematic, explicit and uniform conventions.  Two components:  Top-down and deductive (the overall design).  Bottom-up and inductive (the conventions).

Chair of Software Engineering OOSC - Summer Semester Abstraction and objects  Not all classes describe “objects” in the sense of real-world things.  Types of classes:  Analysis classes – examples: AIRPLANE, CUSTOMER, PARTICLE.  Design classes – examples: STATE, COMMAND, HANDLE.  Implementation classes – examples: ARRAY, LINKED_LIST.  More important than the notion of object is the concept of abstract data type (or “data abstraction”).  Key to the construction of a good library is the search for the best abstractions.

Chair of Software Engineering OOSC - Summer Semester Avoiding improper classes  A few danger signals:  A class whose name is a verb in the imperative form, e.g. ANALYZE. (Exception: command classes.)  A class with no parent and just one exported routine.  A class that introduces or redeclares no feature. (Purely taxonomical role only.) TAXOMANIA  Names that warrant some attention: “er” names, e.g. ANALYZER.

Chair of Software Engineering OOSC - Summer Semester Active data structures  Old interface for lists: l.insert (i, x) l.remove (i) pos := l.search (x) l.insert_by_value (…) l.insert_by_position (…) l.search_by_position (…)  New interface: Queries: l.indexl.iteml.beforel.after Commands: l.start l.forth l.finishl.backl.go (i) l.search (x) l.put (x)l.remove -- Typical sequence: j := l.search (x); l.insert (j + 1, y) Number of features Perfect Desirable ? Number of (re)uses

Chair of Software Engineering OOSC - Summer Semester A list seen as an active data structure item before after count forthback index

Chair of Software Engineering OOSC - Summer Semester An object as machine: “iterators” beforeafter itemindex put_right start forth

Chair of Software Engineering OOSC - Summer Semester Command-Query separation principle  A command (procedure) does something but does not return a result.  A query (function or attribute) returns a result but does not change the state.  This principle excludes many common schemes, such as using functions for input (e.g. C’s getint or equivalent).

Chair of Software Engineering OOSC - Summer Semester Referential transparency  If two expressions have equal value, one may be substituted for the other in any context where that other is valid.  If a = b, then f (a) = f (b) for any f.  Prohibits functions with side effects.  Also:  For any integer i, normally i + i = 2 x i;  But even if getint () = 2, getint () + getint () is usually not equal to 4.

Chair of Software Engineering OOSC - Summer Semester Command-query separation  Input mechanism (instead of n := getint ()): io.read_integer n := io.last_integer

Chair of Software Engineering OOSC - Summer Semester Libraries and assertions  Include as many visible assertions as possible:  Assertions help design the libraries right.  Preconditions help find errors in client software.  Library documentation fundamentally relies on assertions (interface forms). APPLICATION LIBRARY l.insert (x, j + k + 1) i <= count + 1 insert (x: G; i: INTEGER) require i >= 0

Chair of Software Engineering OOSC - Summer Semester Designing for consistency: An example  Describing active structures properly: can after also be before?  Symmetry:  For symmetry and consistency, it is desirable to have the invariant properties. after = (index = count + 1) before = (index = 0) startfinish forthback afterbefore item after count not before not after Valid cursor positions A

Chair of Software Engineering OOSC - Summer Semester Designing for consistency (cont’d)  Typical iteration: from start until after loop some_action (item) forth end  Conventions for an empty structure?  after must be true for the iteration.  For symmetry: before should be true too.  But this does not work for an empty structure (count = 0, see invariant A): should index be 0 or 1?

Chair of Software Engineering OOSC - Summer Semester Designing for consistency (cont’d)  To obtain a consistent convention we may transform the invariant into: after = (is_empty or (index = count + 1)) before = (is_empty or (index = 0) -- Hence: is_empty = (before and after)  Symmetric but unpleasant. Leads to frequent tests of the form if after and not is_empty then...  instead of just if after then... B

Chair of Software Engineering OOSC - Summer Semester Introducing sentinel items  Invariant (partial): 0 <= index index <= count + 1 before = (index = 0) after = (index = count + 1) not (after and before) A not after before not before after itemcountcount not after; not before 1 <= index; index <= count Valid cursor positions

Chair of Software Engineering OOSC - Summer Semester The case of an empty structure not after before not before after 1 (i.e. count + 1)0 Valid cursor positions

Chair of Software Engineering OOSC - Summer Semester Can after also be before?  Lessons from an example; General principles:  Consistency  A posteriori: “How do I make this design decision compatible with the previous ones?”.  A priori: “How do I take this design decision so that it will be easy – or at least possible – to make future ones compatible with it?”.  Use assertions, especially invariants, to clarify the issues.  Importance of symmetry concerns (cf. physics and mathematics).  Importance of limit cases (empty or full structures).

Chair of Software Engineering OOSC - Summer Semester Abstract preconditions  Example (stacks): put is require not full do … ensure … end

Chair of Software Engineering OOSC - Summer Semester Would you rather buy or inherit?  Inheritance is the “is-a” relation.  In some cases “is-a” is clearly not applicable.  Implementation can be a form of “is-a”  Example: the marriage of convenience.

Chair of Software Engineering OOSC - Summer Semester When inheritance won’t do  From: Ian Sommerville: Software Engineering, 4th edition, Addison-Wesley:  Multiple inheritance allows several objects to act as base objects and is supported in object-oriented languages such as Eiffel (Meyer, 1988). The characteristics of several different object classes can be combined to make up a new object.  For example, say we have an object class CAR which encapsulates information about cars and an object class PERSON which encapsulates information about people. We could use both of these to define a new object class CAR- OWNER which combines the attributes of CAR and PERSON.  Adaptation through inheritance tends to lead to extra functionality being inherited, which can make components inefficient and bulky.  Where is the “is-a”?

Chair of Software Engineering OOSC - Summer Semester When inheritance won’t do (cont’d) PERSONCAR CAR_OWNER

Chair of Software Engineering OOSC - Summer Semester The proper structure  PERSONCAR CAR_OWNER

Chair of Software Engineering OOSC - Summer Semester The car-owner “He has a head like an Austin Mini with the doors open.” (From: The Dictionary of Aussie Slang, Five-Mile Press, Melbourne, Australia.)

Chair of Software Engineering OOSC - Summer Semester Would you rather buy or inherit?  Except for polymorphic uses, inheritance is never required:  Rather than having B inherit from A you can always have B include an attribute of type A (or expanded A) – except if an entity of type A may have to represent values of type B. (B)(A)

Chair of Software Engineering OOSC - Summer Semester To be is also to have!  (1) Every software engineer is an engineer.  (2) Every software engineer has a part of himself which is an engineer.  But: TO HAVE IS NOT ALWAYS TO BE!

Chair of Software Engineering OOSC - Summer Semester Would you rather buy or inherit?  A case in which having is not being (i.e. “client” is OK but not inheritance):  Every object of type B has a component of type A, BUT that component may need to be replaced during the object’s lifetime.  Use the client relation instead: class WINDOW inherit GENERAL_WINDOW WINDOW_IMPLEMENTATION feature... end

Chair of Software Engineering OOSC - Summer Semester Handles class WINDOW inherit GENERAL_WINDOW feature handle: TOOLKIT... set_handle (t: TOOLKIT) is do handle := t end... end display is do handle.display (Current) end WINDOWTOOLKIT MS_ WINDOWS GTK handle.display (Current) display+ display* …

Chair of Software Engineering OOSC - Summer Semester Handles (cont’d) class TOOLKIT_FACILITIES feature impl: IMPLEMENTATION is once create Result end set_handle (t: TOOLKIT) is do impl.set_handle (t) end This is a class meant to be inherited by classes needing its facilities.

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