constraints in java

25
MATHEMA AG © 2000 Constraints in Java – OOP 2001 1 Constraints in Java Markus Völter, MATHEMA AG

Upload: barid

Post on 06-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Constraints in Java. Markus Völter, MATHEMA AG. The Problem. Goal: Separation of Responsibilities Server class Client class Server class has well defined interface for use by clients Includes operations, each operations consists of name and parameters - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 1

Constraints in JavaMarkus Völter, MATHEMA AG

Page 2: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 2

The Problem

Goal: Separation of Responsibilities Server class Client class

Server class has well defined interface for use by clients Includes operations, each operations consists of name and

parameters

However, interfaces contain no semantic information (except for types, in some languages)

Page 3: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 3

Examples for the problem

Assumptions about parameters:

Problem: The assumption is not visible in the interface Use of Java Interfaces does not work !!! Implementation can forget to check it Therefore: Client cannot rely on it

Page 4: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 4

Examples for the problem II Semantic consistency in subclasses:

The following sublass-overriding is legal:

Page 5: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 5

Examples for the problem IIb But what about this one:

Problem: A semantic assumption of the super class is broken.

It can be argued that this should not be allowed.

Page 6: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 6

The problem: conclusion

Why does the client assume certain semantics although they are not formally ensured?

It is ok to assume these semantics. Then we need a way to ensure them.

This is the goal of this presentation!

Page 7: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 7

Solution (conceptual): Constraints

Preconditions: Scope: Operation Must be true before operation is executed

Postconditions Scope: Operation Must be true after the operation has been executed

Invariants: Scope: Class Must be true at any time

Page 8: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 8

Constraints in Design (UML)

OCL (Object Constraint Language) can be used to do it!

Page 9: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 9

Constraints in Eiffel

Eiffel provides Constraints: Programming by Contract They can be specified on class interfaces They have different names:

Preconditions: require clause Postconditions: ensure clause

Invariants are also possible, also on interfaces

Page 10: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 10

Constraints in Eiffel II

Eiffel also allows correct constraints in subclasses: Require else Ensure then

Page 11: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 11

Semantic of Constraints

Preconditions: Server requires them to be true Client must ensure this Runtime system checks it Server implementation expects them to be true

Postconditions: Server assures them to be true Server implementation must ensure that Runtime system checks it Client expects them to be true

Page 12: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 12

Constraints in Java

Either do it manually (ifs at the beginning of each method)

Use Precompiler and declarative statements Use Aspects (a kind of precompiler, in some way)

Or, use the following approach...

Page 13: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 13

The Java Proxy API (JDK 1.3) GoF Proxy pattern

Proxy API can dynamically create Proxies for any class in a system (at runtime!)

Forwards any method invocation to an InvocationHandler

Page 14: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 14

Using the API for Constraints

Page 15: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 15

Handle the invocations

Page 16: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 16

Handle the invocations

Page 17: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 17

Specifying Constraints

For an interface X, the constraints are specified in a class Xconstraints

Naming conventions exist for methods: For Operation {visibility} {RetType} op(params) 

precondition is called public void pre_op(params ) For Operation {visibility} {RetType} op(params) 

postcondition is called public void post_op(Object retVal )

The invariant is checked in invariant()

Page 18: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 18

An Example (Interface)

The following is a simple Interface for vehicles:

Page 19: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 19

An Example (Implementation)

The following is a trivial implementation of the interface:

Page 20: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 20

An Example (Constraints)

Precondition: In accelerate(), delta must be > 0

Invariant: Truck must never drive faster than 80 km/h

Page 21: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 21

An Example (Constraints II)

Postcondition: After decelerate, speed must be less than before:

Page 22: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 22

A small disadvantage

To allow Java to create the Proxies, a factory must be used to create instances of classes, of which the constraints should be checked:

Page 23: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 23

A small disadvantage II

Client application needs to use this factory:

Constraint checking can turned on or off easily

Page 24: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 24

Constraint failure

If constraint fails, an Exception is thrown:

Page 25: Constraints in Java

MATHEMA AG © 2000Constraints in Java – OOP 2001 25

Want to know more?

Check out the current issue of

Or ask questions!

Thank you!