modelling small programs modelling large programs modelling application frameworks modelling design...

81
Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z 1 ecture notes in graduate module, CE839: Software Design and Architecture, Autumn term 2008/9 r Amnon H Eden, School of Computer Science and Electronic Engineering, University of Essex

Upload: nancy-baker

Post on 15-Jan-2016

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling small programs

Modelling large programs

Modelling application frameworks

Modelling design patterns

Object-Oriented Modellingin LePUS3 and Class-Z

1

Lecture notes in graduate module, CE839: Software Design and Architecture, Autumn term 2008/9Dr Amnon H Eden, School of Computer Science and Electronic Engineering, University of Essex

Page 2: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

2

LePUS3 and Class-Z: desiderata Abstraction

Abstract ontology Offer an answer: What are the conceptual building blocks of software design?

Scaling: capture the building blocks of very large programs Detailed notation—cluttered diagrams What NOT to model? What a specification should NOT represent?

Rigour Formal specification Verification

Decidability: Tool support in “round-trip engineering” automated verification is possible in principle Tool support automates the verification process Allows us to “close the loop” of round-trip engineering

Visualization (Optional Offer a “picture” of a specification:

Existing program: a ‘roadmap’ to the millions of lines of code A design motif: design pattern, architectural style

Makes software easier to understand and change

Theory & practice Be relevant to practical applications Provide a sound foundation in a solid mathematical theory

Page 3: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

3

Specification language: desiderata(cont.) Combine theory & practice

It has long been my personal view that the separation of practical and theoretical work is artificial and injurious. Much of the practical work done in computing, both in software and in hardware design, is unsound and clumsy because the people who do it have not any clear understanding of the fundamental design principles of their work. Most of the abstract mathematical and theoretical work is sterile because it has no point of contact with real computing. -- Christopher Strachey

Page 4: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Iterators

Aggregates

createIterator

next

Elements

Produce

AggregateReturn

4

What can be modelled in LePUS3/Class-Z?

(Collections in java.util)

Programs

(Java Servlets)

Application frameworks

(The Iterator pattern)

Design patterns

httpServlet

Inhe

rit

A

servletOps

userServlet

servletOps

For

war

d

Col l ecti onsHrc

I teratorsHrc

obj ect

newI tr

next

Produce

Aggregate Return

Page 5: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

5

What cannot be modelled in LePUS3/Class-Z? Temporal relations

‘Method x should be called before method y’ No collaboration/interaction diagrams, flow charts,

statecharts, … Statements about specific objects

Strategic design Architectural styles Components

Programs in procedural/functional/other programming paradigms LePUS3 and Class-Z model object-oriented programs

Page 6: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Tool support in LePUS3and Class-Z

Design

Iterators

Aggregates

createIterator

next

Elements

Produce

AggregateReturn

interface Collection ... { public Iterator iterator() { ... }}

interface Collection ... { public Iterator iterator() { ... }}public class LinkedList ... { public Iterator iterator() { ... }}

public class LinkedList ... { public Iterator iterator() { ... }}

Formal specificationSoftware modellingSoftware visualization

Reverse engineering(Design Recovery)

Forward engineering (Verification)

ApplicationsApp. frameworksClass libraries

Implementation

Page 7: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

7

LePUS3 vs. Class-Z

Specifications can be expressed in one of two ways: LePUS3: Visual, similar to UML Class Diagrams

Class-Z: symbolic, similar to Z

Students are only required to learn one of the notations

printStreamjava.lang.System Member

java.lang.system, printStream :

Example

Member(java.lang.system,printStream)

LePUS3

Class-Z

Page 8: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The genealogy of LePUS3 and Class-Z Definition: As a subset of first-order predicate

calculus (classical logic) Official reference manual:

A.H. Eden, E. Gasparis, J. Nicholson. "LePUS3 and Class-Z Reference Manual". University of Essex, Tech. Rep. CSM-474, ISSN 1744-8050 (2007). http://lepus.org.uk/ref/refman/refman.xml

Historical roots: LePUS3: LePUS [Eden 2001]—LanguagE for Pattern

Uniform Specification Class-Z: Z [Spivey]

8

Page 9: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

9

Modelling small programsin LePUS3 and Class-Z

Class and signature constants

Unary relation symbols

Binary relation symbols

Page 10: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling individual classes

class constant: represents any specific static type such as a class, Java interface, and primitive types

col l ecti onl i nkedLi st i nt

linkedList, collection, int :

Example

class LinkedLi

st

interfaceCollecti

on

primitive typeint

10

Page 11: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling individual methods

signature constant: represents a specific ‘signature’ of (one or more) methods

11

linkedList

newItr

collection

newItr

A method with signature newItr is defined in class Collection

A method with signature newItr is defined in class LinkedList

linkedList, collection : newItr :

Example

Method(newItrlinkedList)Method(newItrcollection)

public class LinkedList ... { public Iterator newItr() { ... }}public class Collection ... { public Iterator newItr() { ... }}

public class LinkedList ... { public Iterator newItr() { ... }}public class Collection ... { public Iterator newItr() { ... }}

Page 12: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling properties

Unary relation: represents a property

12

Class AbstractSet is abstract

abstractSet, collection : newItr :

Example

Abstract(abstractSet)Abstract(collection)Abstract(newItrcollection)

col l ecti on

newI tr

AbstractAbstract

Collection.newItr is an abstract method

Collection is an Interface

Page 13: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

13

Modelling relations between individuals Binary relation: represents a relation between two

individuals

linkedList, abstractSet, ListItr, ListIterator :

Example

Inherit(linkedList,abstractSet)Inherit(ListItr,ListIterator)

abstractSetlinkedList InheritListIteratorListIter

Inherit

LinkedList ‘extends’ AbstractSet

ListIter ‘implements’ ListIterator

Page 14: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

14

Binary relations: Member

public class System {... public static PrintStream out;...}

public class System {... public static PrintStream out;...}

printStreamjava.lang.System Member

java.lang.system, printStream :

Example

Member(java.lang.system,printStream)

Page 15: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Binary relations: Aggregate

obj ectl i nkedLi stAggregate

linkedList, object :

Example

Aggregate(linkedList,object)

15

Page 16: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Binary relations: Call

Note that the Call relation abstracts away all information about the control-flow

16

public class Test { public static void main(String[] args) { if (args.length == 0) System.out.print("Insufficient arguments"); ...

public class Test { public static void main(String[] args) { if (args.length == 0) System.out.print("Insufficient arguments"); ...

test, printStream : main, print :

Example

Call(maintest, printprintStream)

test

main

printStream

Callprint

Page 17: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

17

public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest rq, HttpServletResponse rs) { super.doGet(rq,rs); }

public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest rq, HttpServletResponse rs) { super.doGet(rq,rs); }

Binary relations: Forward

Method MyServlet.doGet forwards the call to HttpServlet.doGet

myServlet, httpServlet : doGet :

Example

Forward(doGetmyServlet, doGethttpServlet)

myServlet

doGet

httpServlet

ForwarddoGet

Forward: A special kind of a Call relation in which the formal arguments are passed on to a method withsame signature

Page 18: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

18

public class MyClass { public void method() { ... if ... else ... ... for (int index = 0; ...) ... }...}

public class MyClass { public void method() { ... if ... else ... ... for (int index = 0; ...) ... }...}

Binary relations: Create

myCl ass

methodi nt

Create

Method MyClass.method may create an integer

linkedList, listItr : listIterator :

Example

Produce(listIteratorlinkedList, listItr)

Page 19: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

19

public class LinkedList { ... public ListIterator listIterator(int index) { if (1 < 0)

return new ListItr(index); } ...}

public class LinkedList { ... public ListIterator listIterator(int index) { if (1 < 0)

return new ListItr(index); } ...}

Binary relations: Produce

linkedList

listIteratorlistItr

Produce

Method LinkedList.listIterator may create and return a new ListItr

linkedList, listItr : listIterator :

Example

Produce(listIteratorlinkedList, listItr)

Produce: A special kind of a Create relation in which the new object is returned(typical to ‘factory methods’)

Page 20: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling indirect relations

Transitive relation: represents possibly indirect relation (the ‘transitive closure’ of a binary relation)

20

collectionlinkedList Inherit+

interface Collection { ...

class AbstractList implements Collection {...

class AbstractSet extends AbstractList ...

class LinkedList extends AbstractSet ...

interface Collection { ...

class AbstractList implements Collection {...

class AbstractSet extends AbstractList ...

class LinkedList extends AbstractSet ...

linkedList, collection :

Example

Inherit+(linkedList, collection)

Page 21: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

21

Transitive relations II

public class FileInputStream ... { public int read(byte[] b) throws IOException, NullPointerException { ...

public class FileInputStream ... { public int read(byte[] b) throws IOException, NullPointerException { ...

fileInputStream, ioException, nullPointerException : read,nullPointerException,ioException :

Example

Call+(readfileInputStream, ioExceptionioException)Call+(readfileInputStream, nullPointerExceptionnullPointerException)

Call+

Call+

nullPointerException

ioExceptionfileInputStream

read

ioException

nullPointerException

Page 22: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

22

Case Study I: LinkedList and ListItr

LinkedList

newItr()

return new ListItr(); Object

Collection

newItr()

Iterator

next()

ListItr

next()

LePUS3 UML Class diagram

l i nkedLi st.l i st I tr

l i nkedLi st

col l ecti on i teratorAbstract Abstract

obj ect

newI trnext

newI trnext

Produce

Return

AbstractAbstract

Aggregate

Inherit+ Inherit+

Page 23: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

23

Case Study II: java.io.XXReader classes

l i neNumberReader

buff eredReader

read

read

mark

mark

reset

reset

Forward Forward Forward

23

bufferedReader, lineNumberReader : read, mark, reset :

ReaderExample

public class LineNumberReader extends BufferedReader { … public void read() { … super.read(); … } … public void mark(int readAheadLimit) { … super.mark(readAheadLimit); … } … public void reset() { … super.reset(); … } …

public class LineNumberReader extends BufferedReader { … public void read() { … super.read(); … } … public void mark(int readAheadLimit) { … super.mark(readAheadLimit); … } … public void reset() { … super.reset(); … } …

Forward(readlineNumberReader, readbufferedReader)Forward(marklineNumberReader, markbufferedReader)Forward(resetlineNumberReader, resetbufferedReader)

Page 24: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

24

Exercise

“Translate” the LePUS3 chart in the case study into a Class-Z specification

Use either Class-Z or LePUS to model three DIFFERENT specifications of the classes Collection, Iterator, LinkedList, and ListItr in the package java.util. Each specification should include one or more of the methods

defined in these classes. Which ones did you choose to model and why?

Note that each specification may contain a different set of relations

Note that each specification must be SATISFIED by the classes in java.util!

Page 25: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

25

Exercise (Cont.)

Which one is the most correct description of the purpose of a LePUS/Class-Z specification?

1. Each specification is model of a particular implementation

2. Each specification models an one or more possible implementations

3. Each specification models an aspect of one implementation abstracted for a particular purpose

4. Each specification models an aspect of one or more possible implementations abstracted for a particular purpose

Page 26: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Exercise (Cont.)

How many ways can you model a Java program in which class MyClass has a method therein with the signature method(String arg) which throws an exception of class MyException in LePUS3 or Class-Z?

26

Page 27: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

27

Modelling large programsin LePUS3 and Class-Z

Hierarchy constant

1-dim class and signature constants

Total and Isomorphic predicates

Page 28: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of classes

1-dimensional class constant: Stands for a (non-empty) set of specific static types

28

ConcreteCollections :

Example

treeSet

hashSet

linkedList

arrayList

ConcreteCol l ecti ons

Page 29: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of methods (one signature)

Clan: A set of methods with same signaturetreeSet

newItr

hashSet

newItr

linkedList

newItr

arrayList

newItr

Concretecollections

newItr

ConcreteCollections : newItr :

Example

All(Method, newItrConcreteCollections)29

Page 30: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of methods (many signatures)

30

1-DimSignatureConstant :

Example

1-dimensional signature constant: Stands for a (non-empty) set of specific method signatures

1-Di m Si gnatureConstant

si g1 si gn

Page 31: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of methods (many signatures) Tribe: A set of methods in same class

31

httpServlet

doGetdoPost

doPutdoTrace

httpServlet

ServletOps

httpServlet : ServletOps :

Example

All(Method, ServletOpshttpServlet)

Page 32: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of methods (many signatures): example

32

buff eredReader

read mark reset

buff eredReader

Buff erOps

BufferOps : bufferedReader :

Example

All(Method, BufferOps bufferedReader)

Page 33: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

33

Modelling relations between sets: Total

Domai n BinaryRelation

Range

d1 r1

d2 r2

d3 r3

d4

BinaryRelation

BinaryRelationEvery element in Domain is in relation

Relation with some element in Range

Total(BinaryRelation,Domain,Range)

Page 34: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

34

Total predicate: exampleIn

heri

t+ Inhe

rit+

collection

Inhe

rit+ In

heri

t+

treeSet

hashSet

linkedList

arrayList

collection

ConcreteCollections

Inhe

rit+

ConcreteCollections : collection :

Example

Total(Inherit+,ConcreteCollections,collection)

Page 35: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Total predicate: example II

ConcreteCollections : object :

Example

Total(Aggregate,ConcreteCollections,object)

treeSet

hashSet

l i nkedLi st

arrayLi st

obj ect

Aggregate

Aggregate

Aggregate

Aggregate

ConcreteCol l ecti ons

obj ect

Aggregate

35

Page 36: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

36

Modelling relations between sets: Isomorphic

Every element in Domain is in relation BinaryRelationwith a unique element in Range

Isomorphic(BinaryRelation,Domain,Range)

Domai n RangeBinary

Relation

d1 r1

d2 r2

d3 r3

d4Abstract

BinaryRelation

Abstract

r4

BinaryRelation

BinaryRelation

Page 37: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Isomorphic predicate: example I

bufferedReader, lineNumberReader : BufferOps :

Example

Inherit(lineNumberReader,bufferedReader)Isomorphic(Forward, ServletOpslineNumberReader,ServletOpsbufferedReader)

l i neNumberReader

buff eredReader

read

read

mark

mark

reset

reset

Forward Forward Forward

buff eredReader

l i neNumberReader

Buff erOps

Buff erOps

Forward

37

Page 38: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Isomorphic predicate: example

II

Forward

Forward

Forward

j ava. awt.component

addPropertyChange

Li stener

appl yComponent

Ori entati on

transferFocus

Backward

j ava. awt.contai ner

addPropertyChange

Li stener

appl yComponent

Ori entati on

transferFocus

Backward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

Forward

nextFocusHel per

nextFocusHel per

i sFocusCycl eRoot

i sFocusCycl eRoot

getMaxi mumSi ze

preferredSi ze

getMaxi mumSi ze

preferredSi ze

removeNoti f y

removeNoti f y

getAl i gnmentX

getAl i gnmentX

di spatchEventI mpl

di spatchEventI mpl

processEvent

processEvent

eventEnabl ed

eventEnabl ed

l i ghtwei ghtPri nt

l i ghtwei ghtPri nt

l i ghtwei ghtPai nt

l i ghtwei ghtPai nt

numLi steni ng

numLi steni ng

getAl i gnmentY

getAl i gnmentY

addNoti f yaddNoti f y

l i stl i st

mi ni mumSi zemi ni mumSi ze

paramStri ngparamStri ng

setFontsetFont

i nval i datei nval i date

pri ntpri nt

j ava. awt.contai ner

j ava. awt.component

Contai nerOps

Contai nerOpsForward

38

Page 39: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Case study: collections & iterators in java.util

treeSet treeMap.KeyI terator

hashSet

l i nkedLi st l i nkedLi st.Li stI tr

arrayLi st abstractLi st . I tr

hashMap.KeyI terator

newI tr

newI tr

newI tr

newI tr Produce

Produce

Produce

Produce

ConcreteCol l ecti ons Concrete

I teratorsnewI tr Produce

ConcreteCollections, ConcreteIterators : Isomorphic(Produce, newItrConcreteCollections, ConcreteIterators)

Example

39

Page 40: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

ConcreteI terators

ConcreteCol l ecti ons

col l ecti on i teratorAbstract Abstract

obj ect

newI trnext

newI trnext

Return

AbstractAbstract

Aggregate

Inherit+ Inherit+

Produce

40

Collections & iterators in java.util (cont.)

treeSet

col l ecti on i terator

treeMap.KeyI terator

hashSet

l i nkedLi st l i nkedLi st.Li st I tr

arrayLi st

abstractLi st . I tr

hashMap.KeyI terator

Abstract Abstract

obj ect

newI trnext

newI trnext

newI tr

newI trnext

newI trnext

next

Inherit+

Produce

Inherit+

Produce

Produce

Produce

Inherit+

Inherit+

Return

Return

ReturnReturn

AbstractAbstract

Aggregate

Aggregate

Aggregate

Aggregate

Inherit+

Inherit+

Inherit+

Inherit+

Page 41: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Exercise

Translate the specification of collections and iterators to UML

41

Page 42: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling class hierarchies

1-dimensional hierarchy constant: a set of classes s.t. all inherit from one

42

Hierarchy :

Example

1-Di m Hi erarchy Constant

OtherCl asses

rootCl s

Inherit+

Page 43: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Hierarchy: example

43

treeSet

col l ecti on

hashSet

l i nkedLi st

arrayLi st

Inherit+

Inherit+

Inherit+

Inherit+

Col l ecti onsHrc

CollectionsHrc :

Example

Page 44: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Modelling sets of methods revisited

44

OtherCl asses

rootCl s

si gHrc

Inherit+si g

si g

Hrc : sig :

Example

All(Method,sigHrc)

Page 45: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

45

Example: the Lists hierarchy

Lists : add :

Example

All(Method,addLists)

arrayLi st

vector

l i st

l i nkedLi st abstractLi st

Abstract

Abstract

addadd

add

add add

Inherit

Inherit Inherit

Inherit

Li stsHrc

add

Page 46: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

46

Case study: collections & iterators in java.util

CollectionsHrc, IteratorsHrc : next, newItr : object :

Example

Isomorphic(Produce, newItrCollectionsHrc, IteratorsHrc)Total(Return, nextIteratorsHrc, object)Total(Aggregate, CollectionsHrc, IteratorsHrc)

ConcreteI terators

ConcreteCol l ecti ons

col l ecti on i teratorAbstract Abstract

obj ect

newI trnext

newI trnext

Return

AbstractAbstract

Aggregate

Inherit+ Inherit+

Produce

Col l ecti onsHrc

I teratorsHrc

obj ect

newI tr

next

Produce

Aggregate Return

Page 47: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

47

Example: lists, sets, and sorted sets

Lists, Sets, SortedSets : newItr : collection :

Example

Total(Inherit+, Lists, collection)Total(Inherit+, Sets, collection)Total(Inherit+, SortedSets, collection)Method(newItrcollection)All(Method, newItrLists)All(Method, newItrSets)All(Method, newItrSortedSets)

Inhe

rit+

col l ecti on

Sets

newI tr

Li sts

newI tr

SortedSets

newI tr

Inhe

rit+ Inherit +

newI tr

Page 48: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

48

Modelling application frameworks

Variables

Page 49: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

49

Variables vs. constants

ObjectaClass

aClass, Object :

Example

A class variable A class constant

Page 50: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

50

Variables vs. constants II

abstractSetlinkedList Inherit

LinkedList extends/ implements AbstractSet

yx Inherit

Class x extends/implements class y

linkedList, abstractSet, x, y :

Example

Inherit(linkedList,abstractSet)Inherit(x,y)

Page 51: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

51

Assignments

A specification with variables is only meaningful wrt a specific assignment

Example: Assignment-1 x is assigned to LinkedList y is assigned to AbstractSet

Example: Assignment-2 y is assigned to LinkedList x is assigned to AbstractSet

Assignment-1 is satisfied by java.util Assignment-2 is satisfied by java.uti

yx Inherit

x, y :

Example

Inherit(x,y)

Page 52: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

52

httpServlet

Inhe

rit

A

ServletOps

userServlet

ServletOps

For

war

d

Example: Java Servlets

Servlet class must extend class HTTPServlet

Servlet class must override a particular set of methods

Each method in your Servlet must forward call to respective method in superclass

Servlet class can be any classthat satisfies therelations

httpServlet, userServlet : ServletOps : ()

JavaServlet

Isomorphic(Forward, ServletOpsuserServlet, ServletOpshttpServlet)Inherit(userServlet, httpServlet)

Page 53: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

53

Variables vs. constants: example I

httpServlet

Inhe

rit

A

doGet

userServlet

doGet

For

war

d

public class HelloText extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { super.doGet(request,response); response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("Hello World"); }}

public class HelloText extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { super.doGet(request,response); response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("Hello World"); }}

httpServlet

Inhe

rit

A

doGet

HelloText

doGet

For

war

d

A specific implementation Any Java Servlet

Page 54: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

54

Variables vs. constants: example II

Iterator pattern

Collections, Iterators : next, newItr : object :

CollectionsInJavaUtil

Isomorphic(Produce, newItrCollections, Iterators)Return(nextIterators, object)

Collections in java.util

Iterator

AggregatesIteratorscreateIteratornextobject

Isomorphic(ProducecreateIteratorAggregatesIteratorsReturnnextIteratorsobject

Iterators

Aggregates

createIterator

next

Elements

Produce

AggregateReturn

Col l ecti onsHrc

I teratorsHrc

obj ect

newI tr

next

Produce

Aggregate Return

Page 55: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Variables vs. constants: example III

Composite patternComposite in java.awt

component

compositeLeaves

ComponentOps

CompositeOps

ComponentOps

Abstract

ComponentOps

Inherit

Forward

AggregateInherit

ComponentLeaves

ComponentOps

contai ner

ComponentOps

Composi teOps

component

ComponentOps

Forward

Abstract

Inherit InheritAggregate

55

Page 56: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

56

Case study: Enterprise JavaBeansFrom [Monson-Haefel 2001]:

1. “Every bean [class] obtains an EJBContext object, which is a reference to the container

2. “The home interface extends the ...javax.ejb.EJBHome interface

3. “A home [interface] may have many create() methods, … , each of which must have corresponding ejbCreate() and ejbPostCreate() methods in the bean class. The number and datatype of the arguments of each create() are left up to the bean developer”

4. “When a create() method is invoked on the home interface, the container delegates the invocation to the corresponding ejbCreate() and ejbPostCreate() methods on the bean class.

5. [An implementation for the bean’s home interface is generated by the container.]”

Page 57: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

57

Modelling Enterprise JavaBeans I

Bean

EJ BContext

A

Mem

ber

j avax. ej b.EJ BHome

A

BeanHomeInterfaceA

Inhe

rit

create

“Every bean [class] obtains

an EJBContext object,

which is a reference to the

container”

“The home interface extends

the ...javax.ejb.EJBHome

interface”

Page 58: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

BeanHomeImp

Beancreate

Call

ejbPostCreateCall

ejbCreate

58

Modelling Enterprise JavaBeans II

“A home [interface] may have many create() methods, … , each of which must have corresponding ejbCreate() and ejbPostCreate() methods in the bean class. The number and datatype of the arguments of each create() are left up to the bean developer

“When a create() method is invoked on the home interface, the container delegates the invocation to the corresponding ejbCreate() and ejbPostCreate() methods on the bean class”

Page 59: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

59

Summary: Enterprise JavaBeans

j avax. ej b.EJ BHome

A

BeanHomeInterfaceA

Inhe

rit

create

BeanHomeImp

GeneratedFrom

Bean

EJ BContext

A

Mem

ber

create

Call

ejbPostCreateCall

ejbCreate

Page 60: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

60

Modelling design patterns

The ‘gang of four’ patterns:Iterator, Proxy, Composite, Observer,

Factory Method, Adapter (Object), Adapter (Class), Strategy

Page 61: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

61

Iterator pattern

Iterator

AggregatesIteratorscreateIteratornextElements

Isomorphic(ProducecreateIteratorAggregatesIteratorsTotalReturnnextIteratorsElementsTotalAggregateAggregatesElements

Iterators

Aggregates

createIterator

next

Elements

Produce

AggregateReturn

Page 62: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

62

Proxy pattern

Proxy

clientsubjectproxyrealSubjectOps-1Ops-2LocalRequestsRemoteRequests

AbstractsubjectTotal(CallOps-1clientLocalRequestssubjectTotal(Call, Ops-2ClientRemoteRequestssubjectTotal(CallRemoteRequestsproxyRemoteRequestsrealSubjectMethod(LocalRequestsproxy)MemberproxyrealSubjectInheritproxysubjectInheritrealSubjectsubject

client subject

Call

LocalRequests

RemoteRequests

CallOps-1

Ops-2

realSubject

RemoteRequests

proxy

Inherit

Member

LocalRequests

Inhe

rit

RemoteRequests Call

A

Page 63: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

63

Composite pattern

Composite

componentcompositeLeavesCompositeOpsComponentOps

AbstractcomponentAll(MethodComponentOpsLeavesAll(MethodCompositeOpscompositeInheritcompositecomponentTotal(InheritLeavescomponentAggregate(compositecomponentIsomorphic(Forward,ComponentOpscompositeComponentOpscomponent

component

compositeLeaves

ComponentOps

CompositeOps

ComponentOps

Abstract

ComponentOps

Inherit

Forward

AggregateInherit

Page 64: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

64

subject

Observers

Members

Inhe

rit

Call

A

concreteSubject

update(subject)

notify

SetState Call

getState

attach(Observers)

constructorCall

Cal

l

detach(Observers) Call

destructor

Observer pattern

Observer

subjectconcreteSubjectnotifygetStateupdateattachdetachconstructordestructorSetStateObservers

AbstractsubjectInheritconcreteSubjectsubjectTotal(CallSetStateconcreteSubjectnotifysubjectTotal(CallnotifysubjectupdateObserversTotal(CallupdateObserversgetStateconcreteSubjectTotal(CallconstructorObserversattachsubjectTotal(CalldestructorObserversdetachsubjectMemberssubjectObservers

Page 65: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

65

Factory Method pattern

FactoryMethod

FactoriesProductsfactoryMethod

Isomorphic(ProducefactoryMethodFactoriesProducts

ProductsFactories

factoryMethod

Produce

Page 66: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

66

Adapter (Class) pattern

ClassAdapter

clienttargetadapteradapteeOperationsRequestsSpecificRequests

AbstracttargetTotal(CallOperationsclientRequeststargetTotal(CallRequestsadapterSpecificRequestsadapteeInheritadaptertargetInheritadapteradaptee

Call

Inherit

targetclient

adaptee

adapter

Operations Requests

Requests

SpecificRequests

Abstract

Call

Inherit

Page 67: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

67

Adapter (Object) pattern

ObjectAdapter

clienttargetadapteradapteeOperationsRequestsSpecificRequests

AbstracttargetTotal(CallOperationsclientRequeststargetTotal(CallRequestsadapterSpecificRequestsadapteeInheritadaptertargetMemberadapteradaptee

Call

Inherit Member

targetclient

adaptee

adapter

Operations Requests

Requests

SpecificRequests

Abstract

Call

Page 68: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

68

Strategy pattern

Strategy

clientcontextrequestalgorithmInterfaceStrategies

MembercontextStrategiesCallrequestclientrequestcontextCallrequestcontextalgorithmStrategiesTotal(CallalgorithmStrategiesInterfacecontext

Strategies

context

client

Interface

algorithm(context)

request request Call

Call

Member

Call

Page 69: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Tool support in LePUS3 and Class-Z

69

Page 70: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Two-TierProgramming

Design

Iterators

Aggregates

createIterator

next

Elements

Produce

AggregateReturn

interface Collection ... { public Iterator iterator() { ... }}

interface Collection ... { public Iterator iterator() { ... }}public class LinkedList ... { public Iterator iterator() { ... }}

public class LinkedList ... { public Iterator iterator() { ... }}

Formal specificationSoftware modellingSoftware visualization

Reverse engineering(Design Recovery)

Forward engineering (Verification)

ApplicationsApp. frameworksClass libraries

Implementation

Page 71: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The Two-Tier Programming Toolkit Round-trip engineering Supports:

Specification & verification (forward engineering) Visualization (reverse engineering)

http://ttp.essex.ac.uk

71

Page 72: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The TTP Toolkit (cont.):Specification in LePUS3

72

Page 73: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The TTP Toolkit (cont.):Specification in Class-Z

73

Page 74: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The TTP Toolkit (cont.): Verification

74

Page 75: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

The TTP Toolkit (cont.): Visualization

75

Page 76: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

76

Exercises

1. Summarize the differences between LePUS3/Class-Z and UML:

1.1 Compare the LePUS3 chart with the UML Class diagram of LinkedList and the ListItr classes

1.2 Use UML to model the Collection and the Iterator hierarchies in Java.util. How is this diagram different from the LePUS3/Class-Z specification?

1.3 Use UML to model Java Servlets. How is this diagram different from the LePUS3/Class-Z specification?

1.4 Use UML to model one of the design patterns. How is this diagram different from the LePUS3/Class-Z specification?

2. List four abstraction mechanisms in LePUS3/Class-Z

Page 77: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Exercises (cont.)

3. Compare the LePUS3/Class-Z Specification of each one of the design patterns to the description in Gamma et al. 1995

3.1 What is the book saying that the chart/schema does not say?

3.2 What is the chart/schema saying that the book does not say?

3.3 What are the advantages of the book over the chart?

3.4 What are the advantages of the chart over the book?

77

Page 78: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

Exercises (cont.)

4. Specify in LePUS3/Class-Z the following statements:

4.1 “There exists a method in class MyClass which creates instances of class String”

Answer:

78

myCl assx Create

stri ng

Page 79: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

LePUS3 quick reference

constants and variables

79

0-dimclass

variable

1-DimClass

Variable

1-DimSignatureVariable

0-dimsignaturevariable

2-DIMHIERARCHYVARIABLE

1-DimHierarchyVariable

2-DI MHI ERARCHYCONSTANT

0-di mcl ass

constant

1-Di mCl ass

Constant

s-di msi gnatureconstant

1-Di mSi gnatureConstant

1-Di m Hi erarchy Constant

Page 80: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

LePUS3 quick reference (cont.)Methods Relations and predicates

80

An individual method

A set of methodsof different

signature

Cl asses

si gHrc

si g

cl s

Si gnatures

cl s

si g

A set of methodsof same

signature

Isomorphicpredicate

Totalpredicate

UnaryRelation BinaryRelation

Domai n RangeBinary

Relation

Domai n RangeBinary

Relation

Page 81: Modelling small programs Modelling large programs Modelling application frameworks Modelling design patterns Object-Oriented Modelling in LePUS3 and Class-Z

References Legend: Key to LePUS3 and Class-Z Symbols [.pdf]

<http://www.lepus.org.uk/ref/legend/legend.xml>

A.H. Eden, J. Nicholson. Object-oriented modelling: Theory and Practice. Unpublished manuscript (ask me for a copy)

A.H. Eden, E. Gasparis, J. Nicholson. "LePUS3 and Class-Z Reference Manual". University of Essex, Tech. Rep. CSM-474, ISSN 1744-8050 (2007). <http://lepus.org.uk/ref/refman/refman.xml>

A.H. Eden, E. Gasparis, J. Nicholson. "The 'Gang of Four' Companion: Formal specification of design patterns in LePUS3 and Class-Z.“ University of Essex, Tech. Rep. CSM-472, ISSN 1744-8050 (2007). <http://lepus.org.uk/ref/companion/index.xml>

A.H. Eden. “Formal Specification of Object-Oriented Design.” Proc. Int'l Conf. Multidisciplinary Design in Engineering CSME-MDE 2001 (21–22 Nov. 2001), Montreal, Canada.

81