constraints in java

Post on 06-Jan-2016

23 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 1

Constraints in JavaMarkus Völter, MATHEMA AG

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)

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 4

Examples for the problem II Semantic consistency in subclasses:

The following sublass-overriding is legal:

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.

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!

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 8

Constraints in Design (UML)

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

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 10

Constraints in Eiffel II

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

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

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...

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 14

Using the API for Constraints

MATHEMA AG © 2000Constraints in Java – OOP 2001 15

Handle the invocations

MATHEMA AG © 2000Constraints in Java – OOP 2001 16

Handle the invocations

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()

MATHEMA AG © 2000Constraints in Java – OOP 2001 18

An Example (Interface)

The following is a simple Interface for vehicles:

MATHEMA AG © 2000Constraints in Java – OOP 2001 19

An Example (Implementation)

The following is a trivial implementation of the interface:

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 21

An Example (Constraints II)

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

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:

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

MATHEMA AG © 2000Constraints in Java – OOP 2001 24

Constraint failure

If constraint fails, an Exception is thrown:

MATHEMA AG © 2000Constraints in Java – OOP 2001 25

Want to know more?

Check out the current issue of

Or ask questions!

Thank you!

top related