Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Design by Contract.
Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Formal Methods in Software Engineering
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 3: Dealing with Objects II.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Design by Contract ™. 2 Design by Contract A discipline of analysis, design, implementation, management.
Feb 2003 R McFadyen1 Contracts (Ch 13) Used to help understand requirements more completely based on assertions; assertions are applicable to any.
Chair of Software Engineering ATOT - Lecture 10, 5 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Jan 2005 Ron McFadyen1 Contracts Used to help understand requirements more completely (and so may not always be necessary) based on assertions;
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer (Nadia Polikarpova) Verification tools.
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 2: Dealing with Objects I.
Software Testing and Quality Assurance
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 11: Design by Contract™
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 3 29 September 2008.
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 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Software Requirements
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 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 5: Project and EiffelStudio Presentation.
ACS-3913Fall 2009 Ron McFadyen1 Contracts Used to help understand requirements more completely (and so may not always be necessary) based on assertions;
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 ATOT - Lecture 2, 2 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chapter 1 Principles of Programming and Software Engineering.
1 Introduction to: Design by Contract Fall 2005 OOPD John Anthony.
Describing Syntax and Semantics
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
© 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 9: Abstraction.
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
SEG4110 – Advanced Software Design and Reengineering
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
UML-1 3. Capturing Requirements and Use Case Model.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 5: Invariants and Logic.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
UML Profile BY RAEF MOUSHEIMISH. Background Model is a description of system or part of a system using well- defined language. Model is a description.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Principles of Programming & Software Engineering
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Used to help understand requirements more completely
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
ISpec: A Compositional Approach to Interface Specification
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class

2 Client, supplier Definitions A client of a software mechanism is a system of any kind — such as a software element, a non-software system, or a human user — that uses it. For its clients, the mechanism is a supplier.

3 Picturing the client relation (See diagram tool of EiffelStudio.) CLIENT SUPPLIER

4 Interface: definition An interface of a set of software mechanisms is the description of techniques enabling clients to use these mechanisms.

5 Kinds of interface User interface: when the clients are people  GUI: Graphical User Interface  Text interfaces, command line interfaces. Program interface: the clients are other software  API: Application Programming Interface (or: Abstract Programming Interface) We’ll now study class APIs.

6 A user interface (GUI)

7 Classes An object (previous lecture) is a software machine allowing programs to access and modify a collection of data Examples:  A city  A tram line  An element of the GUI such as a button Each object belongs to a certain class, defining the applicable operations, or features Example:  The class of all cities

8 Definitions: class, instance, generating class A class represents a category of things. An object represents one of these things. Class A class is the description of a set of possible run-time objects to which the same features are applicable. Instance, generating class If an object O is one of the objects described by a class C, then O is an instance of C, and C is the generating class of O.

9 Objects vs. classes Classes exist only in the software text:  Defined by class text  Describes properties of associated instances Objects exist only during execution:  Visible in program text through names denoting run- time objects Example: Paris

10 Software construction Finding appropriate classes is a central part of software design (the organization of the architecture of a program) Writing down the details is part of implementation

11 A class interface In this discussion “interface” means API (not user interface). We now look at interface of SIMPLE_LINE (simplified version of METRO_LINE ) This will be shown through EiffelStudio (use “Interface” button)

12 A query: “count” How long is this line? See query count Header comment: states purpose of feature “this line”: the instance of SIMPLE_LINE to which count is applied Form of a query declaration: feature_name : RETURN_TYPE INTEGER : a type denoting integer values (e.g. -23, 0, 256) count: INTEGER -- Number of stations on this line

13 Style rule: header comments Don’t even think of writing a feature without immediately including a header comment explaining what it’s about

14 Expressions and their types At run time, every object has a type: its generating class. Examples:  SIMPLE_LINE for the object denoted by Line8  INTEGER for the object denoted by Line8.count In the program text, every expression has a type. Examples:  SIMPLE_LINE for Line8  INTEGER for Line8.count

15 Another query: i_th What is the i-th station of the line? Feature i_th. Convention for consistency: numbering starts at south end i_th (i: INTEGER): METRO_STATION -- The station of index i on this line

16 Two more queries Which are the station at the ends of the line? Properties of every line l :  l. south_end = l. i_th (1 )  l. north_end = l. i_th (l. count) south_end : METRO_STATION -- End station on south side north_end : METRO_STATION -- End station on sorth side

17 Example: class QUERIES class QUERIES inherit TOURISM feature explore_on_click -- Try queries on lines. do Paris. display Console. show (Line8. count) Console. show (Line8. i_th (1)) Console. show (Line8. i_th (Line8. count) end

18 A command: remove_all_stations We want to rebuild Line8 from scratch. We start by removing all stations: Notes:  Our metro lines always have at least one station, even after remove_all_stations  If there is only one station, it is the value of both south_end and north_end remove_all_stations -- Remove all stations except south end.

19 Command extend Adding stations to a line: extend (s : METRO_STATION) -- Add s at end of this line.

20 Class COMMANDS class COMMAND inherit TOURISM feature explore_on_click -- Recreate a partial version of Line 8. do Line8. remove_all_sections -- No need to add Station_Balard, since -- remove_all_sections retains the south end. Line8. extend (Station_Lourmel ) Line8. extend (Station_Boucicaut ) Line8. extend (Station_Felix_Faure ) -- We stop adding stations, to display some results: Console. show (Line8. count ) Console. show (Line8.north_end. name ) end

21 Defining proper interfaces Not every feature is applicable to every possible argument and instance Example: Line8. i_th (200 ) is wrong! The class interface must be precise enough to convey such usage information

22 First try... Add information to the header comment: Better, but still not good enough:  A comment is just an informal explanation  The constraint needs a more official status in the interface i_th (i : INTEGER): METRO_STATION -- The i -th station on this line -- (Warning: use only with i between 1 and count, inclusive.)

23 Contracts A contract is a semantic condition characterizing usage properties of a class or a feature Three principal kinds:  Precondition  Postcondition  Class invariant

24 Property that a feature imposes on every client: i_th (i : INTEGER): METRO_STATION -- The i-th station on this line Precondition require not_too_small: i >= 1 not_too_big: i <= count The precondition of i_th A feature with no require clause is always applicable, as if it had require always_OK: True

25 Assertions not_too_small: i >= 1 Assertion Condition Assertion tag

26 Precondition principle A client that calls a feature without satisfying its precondition is faulty (buggy) software. A client calling a feature must make sure that the precondition holds before the call.

27 Contracts Contracts for debugging Contracts for interface documentation

28 remove_all_stations -- Remove all stations except the South-West end. ensure only_one_left: count = 1 both_ends_same: south_end = north_end Postconditions Precondition: obligation for clients Postcondition: benefit for clients extend (s : METRO_STATION ) -- Add s at end of line. ensure new_station_added: i_th (count ) = s added_at_north: north_end = s one_more: count = old count + 1 Expression value captured on entry

29 old notation Usable in postconditions only Denotes value of an expression as it was on routine entry Example (in a class ACCOUNT) balance : INTEGER -- Current balance. deposit (v: INTEGER) -- Add v to account. require positive: v > 0 do … ensure added: balance = old balance + v end

30 A feature must make sure that, if its precondition held at the beginning of its execution, its postcondition will hold at the end. Postcondition principle A feature that fails to ensure its postcondition is buggy software.

31 What we have seen  Classes  Objects  The notion of interface  GUI vs API  Commands & Queries  Contracts: preconditions & postconditions  Using contracts for debugging