Component-Based Software Engineering Components and Interfaces Paul Krause.

Slides:



Advertisements
Similar presentations
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Advertisements

Design By Contract Using JMSAssert.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy Eivind J. Nordby.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Software Engineering and Design Principles Chapter 1.
Component-Based Software Engineering Components and Interfaces Paul Krause.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Software Testing and Quality Assurance
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Information Hiding and Encapsulation
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 5 : Programming By Contract.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Component-Based Software Engineering Using Interfaces Paul Krause.
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
Design by Contract in Java Concept and Comparison.
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,
Today’s Agenda  Quick Review  Continue on JML Formal Methods in Software Engineering1.
OOSC - JMSAssert. Design By Contract A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
1 Devon M. Simmonds, Computer Science Department Design by Contract Devon M. Simmonds Computer Science Department University of North Carolina, Wilmington.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Component-Based Software Engineering Components and Interfaces Paul Krause and Sotiris Moschoyiannis.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
From Building Blocks to Projects
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Logger, Assert and Invariants
Topics: jGRASP editor ideosyncrasies assert debugger.
Software Development Handing Errors and Creating Documentation
Design by Contract Fall 2016 Version.
CSC 480 Software Engineering
Specifying Object Interfaces
Component-Based Software Engineering
Java Modeling Language (JML)
Computer Science 340 Software Design & Testing
Programming Languages 2nd edition Tucker and Noonan
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Component-Based Software Engineering Components and Interfaces Paul Krause

Lecture 3 - Interfaces Contents  Interfaces as Contracts  Future Proofing  Non-functional requirements  Specifying Pre- and Post-conditions  Using assertions to check Conditions

Components and Interfaces A B1B2 A requires certain functionality to fulfil its obligations. B1 and/or B2 provide that functionality. An interface mediates between clients and providers.

Interfaces as contracts  Can view an interface specification as a “contract” between the client and a provider  So the interface specification must state: What the client needs to do What the client needs to do What a provider can rely on What a provider can rely on What a provider must promise in return What a provider must promise in return What the client can rely on What the client can rely on

Pre- and Post-Conditions  Pre-conditions: What the client must establish before calling the operation What the client must establish before calling the operation The provider can rely on this condition being true whenever the operation is called The provider can rely on this condition being true whenever the operation is called  Post-conditions: What the provider must establish before returning to the client What the provider must establish before returning to the client The client can rely on this condition being true whenever the call to the operation returns The client can rely on this condition being true whenever the call to the operation returns

Example public interface Directory { public void addEntry(String name, File file); // pre name != “” and file != null // post File file = map.get(name) } Associate pre- and post-conditions to every method in the interface

Lecture 3 - Interfaces Contents  Interfaces as Contracts  Future Proofing  Non-functional requirements  Specifying Pre- and Post-conditions  Using assertions to check Conditions

Future proofing  An interface acts as a contract between client and provider  Suppose an interface remains unchanged  Is there any scope for revising an implementation without introducing a risk of “breaking” old clients?  Ans: The revised implementation may require less and/or provide more

Future Proofing public interface Arithmetic { public double processNumbers(double x, double y); // pre x > 5 and y > 5 // post if r = this.processNumbers(x, y) then r > 10 } public double processNumbers(double x, double y) { if ((x < 0) || (y < 0)) throw new IllegalArgumentException(“Args must be >=0”); return x + y ; }

Future Proofing x > 5 and y > 5 implies x >=0 and y >= 0  The new implementation requires less than the interface  The implementation “weakens” the pre- conditions return >= 20 implies return > 10  The new implementation provides more than the interface specifies  The implementation “strengthens” the post- conditions

Lecture 3 - Interfaces Contents  Interfaces as Contracts  Future Proofing  Non-functional requirements  Specifying Pre- and Post-conditions  Using assertions to check Conditions

What else can go wrong? public interface Directory { public void addEntry(String name, File file); // pre name != “” and file != null // post File file = map.get(name) }  strictly this is only a guarantee of partial correctness  the operation either terminates correctly or does not terminate at all!  progress conditions:  something guaranteeing the pre-condition will lead to something guaranteeing the post-condition

Non-functional requirements  We have seen how respecting pre- and post- conditions reduces the risk of a revised implementation “breaking” a client  But are changes in the functional behaviour the only way an implementation can break a client?  Consider a Maths interface, with methods to support an animation package What about a new implementation that improves accuracy? What about a new implementation that improves accuracy?

Specifying the Service Level Now add guarantees for levels of:  Availability;  Mean time between failures;  Mean time to repair;  Throughput;  Data safety;  Capacity.

Time and Space Requirements  Specifying execution time is platform dependent  So what can realistically be included in a contract?  Could use complexity bounds E.g. perform an operation on n elements in n log n time, using at most log n additional storage cells E.g. perform an operation on n elements in n log n time, using at most log n additional storage cells  A client can then determine the absolute time and space bounds for a given provider on a given platform

Lecture 3 - Interfaces Contents  Interfaces as Contracts  Future Proofing  Non-functional requirements  Specifying Pre- and Post-conditions  Using assertions to check Conditions

Back to interfaces?  Design Guideline: “When the functionality supported by a class can be implemented in different ways, it is advisable to separate the interface from the implementation” Xiaoping Jia  And we can cast an object into its interface type if we only want to access its services

List as an example public interface List { public int size( ); public boolean isEmpty( ); public Object element(int i); public Object head( ); public Object last( ); public void insert(Object item, int i); public void insertHead(Object item); public void insertLast(Object item); // more… }

Implementations of List  We can have several different implementations of the List interface: public class LinkedList implements List { }  or: public class DynamicArray implements List { }

Interfaces as Contracts  Interfaces specify the types of their associated methods, but say nothing about their behaviour  We can improve on that!

Pre- and Post-Conditions  Pre-conditions: What the client must establish before calling the operation What the client must establish before calling the operation The provider can rely on this condition being true whenever the operation is called The provider can rely on this condition being true whenever the operation is called  Post-conditions: What the provider must establish before returning to the client What the provider must establish before returning to the client The client can rely on this condition being true whenever the call to the operation returns The client can rely on this condition being true whenever the call to the operation returns

Stage 1: Documentation  Use and as special tags for preconditions and postconditions: /** precondition postcondition */ */ public void method1 { // … }

A boring example /** * Returns the number of elements in a list * */ public int size( );

A more interesting example /** * Returns the i-th element in the list * i >= 0 && i = 0 && i < size( */ public Object element(int i);

Other logical expressions ==>logical implication logical equivalence logical x : Expression Universally quantified x : Expression [m … n]Integer range from m to n

A nasty example /** * Inserts a new element into a list at the i-th position * item != null && i >= 0 && i = 0 && i <= size( ) size( ) == size( + k : [0.. size( ) - *(k == element(k)) && *(k == i ==> == element(k)) && *(k > i ==> == element(k) */ public void insert(Object item, int i);

An exercise Use this method to specify the interface to a method that inserts a new element to the head of a list

Solution /** * Inserts a new element at the head of a list * item != null size( ) == size( + 1 == element(0) k : [1.. size( ) - == element(k) */ public void insertHead(Object item);

How does this help?  As this stands, the comments provide guidance only  They prescribe what should be done, but are agnostic about whether a specific implementation satisfies them  We can, however, provide run-time checks through the use of Assertions

Lecture 3 - Interfaces Contents  Interfaces as Contracts  Future Proofing  Non-functional requirements  Specifying Pre- and Post-conditions  Using assertions to check Conditions

Assertions are?  Boolean conditions that are inserted into a program at strategic points  We expect them to be true when the flow of control reaches each respective point  If an assertion is true, it has no effect and the flow of control continues  If an assertion is false, then execution stops at that point and an AssertionError is thrown

Where do you put them?  Assertions on the preconditions of a method should be placed immediately on entry to the method  Assertions on postconditions should be placed just prior to the return statement  But remember that any references to the pre-state of variables must be instantiated upon entry to the method

LinkedList implements List /** item != null && i >= 0 && i = 0 && i <= size( ) size( ) == size( + 1 */ public void insert(Object item, int i) { assert item != null && i >= 0 && i = 0 && i <= size( ); int size_pre = size( ); // // // // int size_post = size( ); assert size_post == size_pre + 1; }

Extra points to note  Need JDK 1.4 or higher, and compile using: javac -source 1.4 Class.java javac -source 1.4 Class.java  Execute the class with: java -ea Class java -ea Class  In order to fully assert the postconditions, we usually need to clone an instance of a class before we modify it  Use of assertions derived from preconditions is known as Defensive Programming  Use of assertions derived from postconditions is also valuable in testing and debugging

Summary  We have discussed the use of interfaces when there may be multiple ways of implementing certain functionality  Including statements of pre- and post-conditions in the comment lines for each method declaration makes a richer specification  Including the pre- and post-conditions in the implementation of each interface adds a further level of rigour (but has limited support in Java at present)