Document UML java

32
38 2.2 Class diagrams in Java I The static information of a class diagram can be translated directly into Java. I The code skeleton has no implemented methods. 2.2.1 Declaring Classes and Interfaces UML Java K K {abstract} abstract class K {...} << interface >> I interface I {...} class K {...}

description

Document UML java

Transcript of Document UML java

Page 1: Document UML java

382.2 Class diagrams in Java

I The static information of a class diagram can be translated directly intoJava.

I The code skeleton has no implemented methods.

2.2.1 Declaring Classes and Interfaces

UML Java

K

K{abstract}

abstract class K {...}

<< interface >>I

interface I {...}

class K {...}

Page 2: Document UML java

39UML Java

A

B

<< interface >>I

K

class A {...}

interface I {...}

class K implements I {...}

class B extends A {...}

Page 3: Document UML java

402.2.2 Declaring Attributes

UML Java

attribute:Type JavaType attribute;

The standard types of UML are translated as follows to Java.

UML Java

Boolean booleanInteger intReal float or doubleString String

Page 4: Document UML java

412.2.3 Declaring Methods

UML Java

op(x: Type) void op(JavaType x) {...}op(x: Type): ResType JavaResType op(JavaType x) {...}op() {abstract} abstract void op();

K(x: Type) K(JavaType x) {...} // constructor

Page 5: Document UML java

422.2.4 Defining Access Rights

UML Java

- private //accessible within the class

] protected //accessible in subclasses and in the package

+ public //accessible outside the class

⇠ //java default: accessible in the package

Page 6: Document UML java

432.2.5 Defining Directed Associations

UML Java

B

B

...+addB(b: B)

...

*A−myBs

public void addB(B b) { myBs.add(b); } ...}

class A { private B myB; //Referenzattribut ...

...

...

A−myB

+set(b: B) public void set(B b) { myB = b; } ...}

private Set<B> myBs = new HashSet<B>();

import java.util.*;

class A {

...

//Set = Interface für Mengen//HashSet = Implementierung v. Set

0..1

Page 7: Document UML java

44UML Java

B

key: T

A

A B0..100−myBs

class A { private B[] myBs = new B[100]; ...}

+addB(key: T, b: B)

...

+selectB(key: T): B...

−myBs0..1

}

...}

public B selectB(T key) {

} myBs.put(key, b); public void addB(T key, B b) {

...

class A {

import java.util.*;

private Map<T,B> myBs = new HashMap<T,B>();

return myBs.get(key);

//Map = Interface für Schlüssel/Element−Paare

Page 8: Document UML java

452.2.6 Defining Bidirectional Associations

A B

} return myB; public B getMyB() {

}

myB = b; public void relate(B b) {

public void unrelate() { myB.unset(); myB = null; }}

myB.setMyA(this);

class B { private A myA;

return myA; }

public A getMyA() {

myA = a; }

void setMyA(A a) {

myA = null; }

void unsetMyA() {

}

class A { private B myB;

−myB0..1−myA

0..1

Page 9: Document UML java

462.2.7 Example (Class Diagram)

1 number: Integerbal: Real

getNumber(): Integer+getBal(): RealgetOwner(): Persondeposit(d: Real)

++++

##

# owner−−

Person

name: Stringsalary: Real

SavingsAccount

− interestRate: Real

+ CheckingAccount(no: Integer, cR: Real, o: Person)+ withdraw(d: Real): Boolean+ addInterest() + payCharge()

+ withdraw(d: Real): Boolean

− chargeRate: Real

CheckingAccount

withdraw(d: Real): Boolean {abstract}

BankSimulation

main(String[] args)+ addAccount(a: Account)++ selectAccount(no: Integer): Account

+ SavingsAccount(no: Integer, iR: Real, o: Person)

Account{abstract}

− accounts*

+ getSalary(): Real+ getName(): String+ Person(n: String, s: Real)

Bank

*

Page 10: Document UML java

47Example (Code Skeleton)

class BankSimulation {public static void main(String[] args) {

// to be filled}

}import java.util.*;class Bank {

private Set<Account> accounts = new HashSet<Account>();public void addAccount(Account a) {

// to be filled}public Account selectAccount(int no) {

// to be filled}

}class Person {

private String name;private double salary;public Person(String n, double s) {

// to be filled}public String getName() {

// to be filled}public double getSalary() {

// to be filled}

}

Page 11: Document UML java

48

abstract class Account {protected int number;protected double bal;protected Person owner;

public int getNumber() {// to be filled

}public double getBal() {

// to be filled}public Person getOwner() {

// to be filled}public void deposit(double d) {

// to be filled}public abstract boolean withdraw(double d);

}class SavingsAccount extends Account {

private double interestRate;public SavingsAccount(int no,double iR,Person o) {

// to be filled }public boolean withdraw(double d) {

// to be filled }public void addInterest() {

// to be filled }}class CheckingAccount extends Account {...}

Page 12: Document UML java

492.3 Modelling of Dynamic Behaviour

Techniques

IInteraction diagrams:

describe the communication and cooperation of several objects.

IState diagrams:

describe the behaviour of one object of a certain class at runtime.

IActivity diagrams:

describe (possibly parallel) traces of activities.

Page 13: Document UML java

502.3.1 States and Events

States

I A state is a situation during the lifetime of an object during which somecondition is satisfied:

I The object is performing some activity (do activity).I The object is waiting for some event to trigger a change in state.

I When the object satisfies the conditions for a state, the state is said to beactive.

I In general, an object is associated with a set of active states.

I A state machine specifies the sequence of states that an object may gothrough during its lifetime.

Notation

I States are represented graphically by a rectangle with rounded corners.

I It may optionally have a name (a string).

Page 14: Document UML java

51Event = something which happens at a certain time point.

Ereignis 1

Ereignis 2

Zustand C

Ereignis 3

Zeit

Zustand A

Zustand B

Event Kinds

I Signal event (e.g., pushing a button, open a door)

I Call event (calling a function, e.g., myKonto.einzahlen(1000))

I Change event (e.g., when (temperature < 0))

I Time event (e.g., after(5 sec))

I Completion event (e.g., downloading a file)

Note:

Events have a duration (in contrast to states)!

Page 15: Document UML java

522.3.2 Flat State Diagrams

Directed graph with

I nodes = states

I arcs = transitions

Transition

describes an event-induced change from the source state ZA to the target stateZB.

ZA ZB

Ereignis

Page 16: Document UML java

53Example: Automatic Gearbox

Leerlauf Rückwärts

1. Gang 2. Gang 3. Ganghochrunter

hochrunter

rückleer

vor leer leerleer

Remarks

I A state with no transitions for a certain event ignores such an event.

I The symbol denotes the initial state (”pseudo state”).

I The symbol denotes a final state (destruction of the object ortermination of an activity).

Page 17: Document UML java

54Guards

I A condition ( boolean statement) can be used as a guard for a transition.

I The transition fires if the event occurs and the condition holds.

Syntax:

ZA ZB

Ereignis[Wächterbedingung]

Activity

I Action (which may need time).

I Can be a response to an event.

Syntax:

ZA ZB

Ereignis/Aktivität

Note:

An activity on the transition arc cannot be interrupted by an event.

Page 18: Document UML java

55Example:

Konto überzogen

Konto ausgeglichen

einzahlen(b)[saldo+b<0]/saldo=saldo+b

auszahlen(b)[saldo!b<0]/saldo=saldo!b einzahlen(b)[saldo+b>=0]/saldo=saldo+b

einzahlen(b)/saldo=saldo+b

auszahlen(b)[saldo!b>=0]/saldo=saldo!b

Page 19: Document UML java

56General Transition Syntax

ZA ZB

Ereignis(Argumente)[Bedingung]/Aktivität

General State Syntax

Eingangsaktivität:Wird immer beim Eintrittin den Zustand ausgeführt;ist nicht unterbrechbar

interne Transition:Ereignis löst keinenZustandsübergang aus;entry/exit−Aktivitätenwerden nicht durchgeführt;Aktivität ist nicht unterbrechbar

Zustandsname

Ereignis/Aktivitätdo/Aktivitätexit/Aktivität

verzögertes Ereignis/defer

entry/Aktivität

do−Aktivität:Wird ausgeführt solangesich das Objekt in demangegebenen Zustandbefindet;

ist nicht unterbrechbar

Ausgangsaktivität:Wird immer beim Verlassendes Zustands ausgeführt;

ist unterbrechbar

Page 20: Document UML java

57Example: Telephone

Bereit

Verbunden

after(1 min)

do/läuten

Klingelnd

einhängenabheben

Note:A (perhaps conditional) completion event arises if the do activity terminates onits own.

W V

X

[not B]

e

[B]do/activity

Z

Page 21: Document UML java

58Delayed Event

Z

e/defer

An event e is stored and processed later in some state which can handle e, ifthe event e arises before in a state Z which has no outgoing e transitions.

Example: Bu↵er with one element

leer voll

put

get

get/defer put/defer

Page 22: Document UML java

592.3.3 Hierarchical State Diagrams

A state can be refined into substates.

1. Sequential Substates

Leerlauf Rückwärts

Vorwärts

2. Gang1. Gang 3. Gangrunter

hoch

runter

hoch

vor

rück

leer

leer

I A transition in a superstate (Vorwarts) refers to a transition in the initialstate of the nested diagram (1. Gang)

I A transition from a superstate refers to a transition from a containedsubstate.

Page 23: Document UML java

60Abstract representation of a complex state:

Leerlauf

Vorwärts

Rückwärts

vor leer

rück

leer

Remark

Complex states can be equipped with entry and exit points.

Page 24: Document UML java

612. Parallel Substates

A B

C

E

D

Z

a

c

fail

V e

W2

W3

W1

w

I An object is in several states at the same time. Entering the superstatemeans therefore that the object is in the initial state of each region.

I The superstate will be exited if one reaches the final state in each singleregion, or there is a direct outgoing transition from a substate, or there isan outgoing transition from the superstate originating from an explicitevent.

Page 25: Document UML java

622.3.4 Activity Diagrams

Can be used to describe the behaviour of

I business processes

I use cases

I operations and processes

An activity diagram is a directed graph which contains

Iactivity nodes: describe actions, control structures and data.

Iactivity arcs: connect activity nodes, inducing therefore traces.

Remark

Activities which are expressed as entry, exit and do activities with respect to astate can be modelled by activity diagrams in a more precise way.

Page 26: Document UML java

63Example: Business process “Order Processing”

Auftrag annehmen

Rechnungstellen

Bezahlen

Ausliefern

Artikelbeschaffen

Parallelisierungsknoten(fork)

Synchronisationsknoten(join)

Auftragsbearbeitung

Page 27: Document UML java

64

aufnehmenKundendaten

aufnehmenBestellartikel

Kunden eingebenName des

[Kunde vorhanden]

[Kunde nicht vorhanden]

Auftrag annehmen

Bemerkung

Case distinctions can be modelled by decision nodes.

Page 28: Document UML java

65Conclusion of Section 2.3

I State diagrams describe the behaviour of each object from a certain classduring its lifetime.

I A transition refers to the state change caused by an event.

I Events are in contrast to states (and activities) timeless.

I We distinguish between five di↵erent kinds of events.

I Events can be guarded and an event can be followed by an activity.

I State diagrams can have a hierarchical structure.We distinguish between

I sequential substatesI parallel substates

I Activity diagrams describe traces of activities.

Page 29: Document UML java

662.4 Meta Modelling

All concepts which are used in an UML model (e.g., class, operation, state,activity, ...) can be described by a class model.

Example: A metaclass has classes as instances

Class

I The meta model specifies all valid UML models.

I This gives usI a tool for checking the syntactical correctness of UML modelsI a basis for generic formats (XMI)

I The meta model can be extended to business modelling, web engineering,...

Page 30: Document UML java

67A section of an UML meta model

ownedAttribute* class0..1

<<enumeration>>VisibilityKind

privateprotectedpublicpackage

TypedElement PackageableElement

isStatic: Boolean = false

Feature

BehavioralFeature

Operation

isQuery: Boolean = false

StructuralFeature

isReadOnly: Boolean = false

Property

Classifier

isAbstract: Boolean = false

ownedOperation*

Type

type 0..1

Package

Class DataTypeInterface

class0..1

RedefinableElement

visibility: VisibilityKind [0..1]

NamedElement

name: String [0..1]

Page 31: Document UML java

68A possible application of the meta model

type

isAbstract=false

:DataType

name="Integer"visibility = public

Darstellung der Klasse Counter als Instanzendiagramm des Metamodells

Counter

isAbstract=true

:Class

−count: Integer {readOnly}

ownedAttributeclass

isStatic=falseisReadOnly=true

:Property

name="Counter"name="count"visibility = private

{abstract}

Page 32: Document UML java

69Meta model for state diagrams(UML 2.2 Superstructure Specification)

UML Superstructure Specification, v2.2 527

Package BehaviorStateMachines

Figure 15.2 - State Machines