05-ClassDiagrams

49
Analyse et Méthodologie Informatiques Roel Wuyts ULB 2005/2006 http://decomp.ulb.ac.be/education/ami0506/ UML Class Diagrams

description

tanks stupid scribd ;>

Transcript of 05-ClassDiagrams

Page 1: 05-ClassDiagrams

Analyse et Méthodologie Informatiques

Roel WuytsULB

2005/2006

http://decomp.ulb.ac.be/education/ami0506/

UML Class Diagrams

Page 2: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 2

Class Diagrams

Static model type

✦ A view of the system in terms of classes and relationships

Classes not only describe attributes but also behaviour !

Description of object types .

✦ Attributes and behaviour of a type of objects

✦ All objects are instances of a certain class

Page 3: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 3

A rectangle divided into 3 compartments :

✦ name, attributes, operations

✦ Let’s have a look at these three compartments...

A Class in UML…

Name

attributes

operations

Page 4: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 4

A Class in UML…

The name compartment

✦ Starts with an uppercase

✦ Boldface

✦ Most of the time a noun

Page 5: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 5

A Class in UML…

The attributes compartment

✦ Starts with a lowercase

✦ Has a type (String, Integer, …)

✦ Can have a visibility

‣ public (+), private (-), protected (#)

✦ Default values can be specified

✦ Allowed values can be specified ({...})

✦ Class scope is possible (underlined)

Page 6: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 6

A Class in UML…

Invoice

+ amount : Real+ date : Date = Current date+ customer : String- administrator : String = "Unspecified"- number of invoices : Integer+ status : Status = unpaid {unpaid, paid}

Page 7: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 7

A Class in UML…

The operation compartment :

✦ Contains the signature of the operation

‣ a return type

‣ a name

‣ zero or more parameters

✦ Can have a visibility

‣ public (+), private (-), protected (#)

Page 8: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 8

A Class in UML…

Figure# size : Size# pos : Position+ draw()+ scaleFigure(percent : Integer = 25)+ returnPos() : Position

Page 9: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 9

Relationships Between Classes

An association is a connection between classes

✦ “usage”

A generalization is a relationship between a more general and a more specific element

✦ “inheritance”

A refinement is a relationship between two descriptions of the same thing but at different levels of abstraction

A realization is a relationship between elements where one carries out what the other specifies

Page 10: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 10

Associations

Specify structural relationships

Specifies that objects are interconnected

Can be implemented in a lot of ways

✦ through instance variables

✦ through arguments of methods

Page 11: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 11

Association Relationship

Drawn as line between classes

By default bidirectional

✦ but particular direction can be indicated

Can contain multiplicities

✦ a range that tells us how many objects are linked

9 examples to show different aspects and give some additional information...

Page 12: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 12

Example 1

Model the following:

✦ An author can use a computer.

Page 13: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 13

Example 1 Solution

association

associationname

associationdirection

Author Computeruses

Page 14: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

On association directions...

14

Author Computeruses

Author Computeruses

Author Computeruses

Author Computeruses >

✕✓

Page 15: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 15

Example 2

Model the following:

✦ A user can own 0 or more cars. Each car is owned by 1 or more persons

Page 16: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 16

Solution Example 2

Multiplicities

A Person Owns 0 or many CarsA Car is Owned by 1 or many Persons

Person CarOwns

Owned By0..*

1..*

Page 17: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 17

Common Multiplicities

(See primitive types for all possibilities)

Multiplicity Notation

optional 0..1

zero or more 0..* or *

at least one 1..*

exactly one 1 or left blank

Page 18: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 18

Example 3

Model the following:

✦ Nodes can be connected to other nodes.

Page 19: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 19

Recursive association: connecting a class to itself

Recursive Associations

Node

Connects

*

*

Page 20: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 20

Example 4

Model the following:

✦ A Canvas contains many Figures which are identified by an identifier

Page 21: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 21

Specifies how a certain object at the many end is identified (+/- a key)

A Canvas contains many Figures which are identified by a figure id

Qualified Associations

Canvas Figure*figure id

Page 22: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

Qualified associations as restrictions

Qualified association subdivides the referenced set of objects into partitions where, viewed from the initial object, each partition may occur only once

Example: employees are partitioned: all employees with the same initials belong to one partition

22

Company

nameaddress

Employee

nameemploys >

*1initials

company initials employee

ULB RW Roel Wuyts

ULB TM Thierry Massart

UMH TM Tom Mens

Page 23: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 23

Example 5

Model the following:✦ An insurance company has insurance contracts, which

refer to one or more customers.

‣ A customer has insurance contracts (zero or more), which refer to one insurance company.

‣ An insurance contract is between an insurance company and one or more customers. The insurance contract refers to both a customer (or customers) and an insurance company.

‣ The insurance contract is expressed in an (zero or one) insurance policy (a written contract of insurance). The insurance policy refers to the insurance contract.

Page 24: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 24

Example 5 solution

When we model very complex systems, especially information

or business systems, it is very important to communicate the

results (the model). If we can communicate our model, then it

is possible at an early stage to verify, validate, and consolidate

it. A class diagram is more unambigious than text. When

creating the diagram, many decisions must be made that

otherwise would not be. Even a small model contains a lot of

information, and it is always possible to translate the model

into natural language.

For example, the model in Figure 7 can be expressed in

natural language:

is expressed in !

"expresses an

has#

$refers to

! has

refers to "

Figure 7 Insurance Business Class Diagram

Insurance policy

Insurance contract

Customer

Insurance company

Page 25: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 25

Example 6

Model the following:

✦ An Insurance contract belongs to a Person or to a Company

Page 26: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 26

Put constraint between the associations

A contract belongs to a Person or a Company

Example 6: ‘or’ associations

constraintInsurancecompany

Insurancecontract

Person

0..*

1..*

Company

0..*

1..*

{or}

Page 27: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 27

Example 7

Model the following:

✦ A customer can have multiple insurances. An insurance has an ordered collection of 1 or more customers.

Page 28: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 28

Example 7: ordered association

constraintInsurancecontract

Customer

0..*

1..*

{ordered}

Page 29: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 29

Other constraints

Some widely used constraints:

✦ {ordereded}, {or} (same as {xor})✦ {implicit} : specifies that the relationship is not

manifest but, rather, is only conceptual✦ {changeable}: Links between objects may be added,

removed, and changed freely✦ {addonly}: New links may be added from an object on

the opposite end of the association✦ {frozen}: A link, once added from an object on the

opposite end of the association, may not be modified or deleted

Page 30: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 30

Example 8

Model the following:

✦ An elevator control manipulates four elevators. On each link between the elevators and the elevator conrol, there is a queue. Each queue stores the requests from both the elevator control and the elevator itself. When the elevator control chooses an elevator to perform a request from a passenger outside the elevator, the elevator control reads each queue and choses the elevator that has the shortest queue. The choice could also be made using some clever algorithm.

Page 31: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 31

Example 8: association classes

class Queue is needed for the association (can

have state/methods needed to implement the

algorithm)

Elevator

Queue

Elevatorcontrol

Button

*

4

Page 32: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

Example 9

A reservation on a passanger train consists of a passanger (for whom the seat is reserved), a seat (which is being reserved), and a train (time of reservation). Besides such simple reservations, group reservations are also allowed. Who will actually occupy which seat is left over to the group.

32

Page 33: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 33

Example 9: ternary associations

ternary association

Traindatenumber

SeatCarriageNumberNumber

1..*1

Passengername

title

1..*

Reservation

Page 34: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

n-ary associations

Associations in general relate n classes

✦ Most of them are binary, but three (as in previous example) or more are possible

Can usually be transformed in binary associations:

34

Traindatenumber

SeatCarriageNumberNumber

1..**1 *

Passengername

title

1..*

*

Reservation

Page 35: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 35

A whole-part association (whole owns the part)

Describes different levels of abstraction

Aggregation

whole part

aggregation

Note: Diamond can only be at one end!

NavyContains

* Warship

Page 36: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 36

The parts can only exist if the whole exists

✦ they are destroyed with the whole

Composition Aggregation`…

* Text

Window

Listbox

Button

Menu*

*

*

Page 37: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 37

Other notations expressing the same:

Composition Aggregation…

* Text

Window

Listbox

Button

Menu*

*

*

Contains

Window

Text

Listbox

Button

Menu

*

*

*

*

Page 38: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 38

Shared Aggregation

Part may be part of several wholes

Shared Aggregation

A shared aggregation is one in which the parts may be parts in

any wholes, as shown in Figure 18. That an aggregation is

shared is shown by its mul tiplicity. The aggregation is shared

if the multiplicity on the wholeside is other than one (1).

Shared aggregation is a special case of a normal aggregation.

* *

Members

Figure 18 A Shared Aggregate Relationship

Figure 19 shows a remix is composed of many sound clips; the

same sound track could be a part of many remixes.

* {ordered} *

Contains

Figure 19 A Shared Aggregate Relationship

Team Person

Remix Sound clips

Shared Aggregation

A shared aggregation is one in which the parts may be parts in

any wholes, as shown in Figure 18. That an aggregation is

shared is shown by its mul tiplicity. The aggregation is shared

if the multiplicity on the wholeside is other than one (1).

Shared aggregation is a special case of a normal aggregation.

* *

Members

Figure 18 A Shared Aggregate Relationship

Figure 19 shows a remix is composed of many sound clips; the

same sound track could be a part of many remixes.

* {ordered} *

Contains

Figure 19 A Shared Aggregate Relationship

Team Person

Remix Sound clips

* : shared aggregation

Page 39: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 39

Association or Aggregation?

The decision to use association or aggregation is a matter of judgment and is often arbitrary

E.g. What type of relationship should be used to model a car with its tires?

✦ If the application is a service center, the only reason you care about the tire is because it is part of the car, so use an aggregation

✦ If the application is a tire store, you will care about the tire independent of the car, then the relationship should be an association.

Page 40: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 40

Recap: associations

Connect classes

✦ between 1, 2, 3, ... classes

Can contain multiplicities, directions, role names

Constraints can be used to express additional requirements (more on constraints later)

Association classes can be used for more difficult associations that have behaviour from their own

Aggregation: part-whole composition

Page 41: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 41

Inheritance, ‘is-a’ relationship

The more specific may be used where the more general is allowed

Remember : Using Inheritance for code reuse, or just because it looks nice is a dangerous practice !

Generalization…

Boat TruckCar

Vehicle

Page 42: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 42

Generalization

Abstract classes & Polymorphism

Works because of late binding of self/this

Page 43: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 43

Refinement

“A relationship that represents a fuller specification of something that has already been specified at a certain level of detail.”

Usefull for modelling

✦ a relation between the analysis version and the design version

✦ a relation between a clean implementation and an optimized but potentially difficult variation

Page 44: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 44

Class refinement examples

comprises two dimensions: intension and extension, where the intension of a modeling artifact is equated with its definition or specification, while its extension refers to the set of elements that fall under that definition.

2. Class Refinement

Classes serve as specifications for the properties of sets of objects that can be treated alike. The intension of a Class is defined as a pair (Attr, Ops) where Attr={a1,...an} is a set of Attribute’s description and Ops= {op1,...opm} is a set of Operation’s description.

Figure 1a shows the UML artifact specifying abstraction/refinement relationship between Classes. The Catalysis methodology (d’Souza and Wills, 1998) mentions that refinement between Classes (or Types) can be realized in two different ways:

a) Attribute (or model) Refinement: The refined Class, B, is obtained by adding a new attribute, attrk, to the abstract Class A. That is to say, B = A + ({attrk}, {})1. Other case takes place when the Class B is obtained from Class A=( {a1, ..., ak,..., an} , Ops) by replacing an attribute ak by its refinement, that can be one or more new attributes, ak1, ..., akl. That is to say, B= A[ak | ak1, ..., akl ]2. For example, figure 1b shows that the attribute length in Class Segment is refined by the attributes xinitial and xfinal through the mapping <length = xfinal – xinitial>.

b) Operation Refinement: The refined Class, B, is obtained by adding a new operation, opk, to the abstract Class A. That is to say, B = A + ({}, {opk}). On the other hand the Class B can be obtained from Class A=(Attr, {op1, ..., opk,..., opn}) by replacing an operation opk by its refinement, that can be one or more new operations, opk1, ..., opkl. That is to say, B= A[opk | opk1, ..., opkl]. For example, figure 1c shows that the operation stretch in Class Segment is refined by the operations moveXini and moveXfin through the mapping <stretch(w) = moveXini(-w/2) ; moveXfin(w/2)>.

(a)

A

B

<<refine>>mapping=...

(b)

Segmentlength : Number

stretch(k : Integer)

Segment'xinitial : Numberxfinal : Number

stretch(k : Integer)

length = xfinal - xinitial

<<refine>>

(c)

Segmentlength : Number

stretch(k : Integer)

Segment'xinitial : Numberxfinal : Number

moveXini(w)moveXfin(w)

stretch(k) = moveXini(-k/2) ; moveXfin(k/2)<<refine>>

stretch(k)::pre:k>0post:length=length@pre+k

moveXini(w)::post:xinitial=xinitial@pre+wmoveXfin(w)::post:xfinal=xfinal@pre+w

Figure 1: Abstraction/Refinement relationship between Classes. (a) UML notation. (b) Attribute refinement. (c) Operation refinement.

A refined Class is specified in a language that is richer than the language of its abstraction. However, there exists always a mapping from the abstract to the refined language (figure 2a), usually called “implementation mapping” [Cardelli and Wegner, 1985]. This mapping makes it possible to translate OCL expressions written in the abstract language to the refined language, for example, the OCL expression (Context Segment inv self.length > 0) can be translated to the expression (Context Segment’ inv self.xfinal – self.xinitial > 0).

1 Addition of Class intension is defined in the usual way: (A,O) + (A’,O’) = (A!A’, O!O’). 2 B[y | x1,...,xn ] denotes the replacement of element y in B by elements x1,.., xn.

Page 45: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 45

Realization

“A semantic relationship between classifiers in which one classifier specifies a contract that another classifier guarantees to carry out”

realization

IRuleAgent

addRule()changeRule()explainAction()

AccountBusinessRules

service description implementor

Page 46: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

Implementing realization

In Java: implemented with interfaces

In C++: implemented with fully abstract classes

46

<<interface>>IRuleAgent

addRule()changeRule()explainAction()

AccountBusinessRules

IRuleAgent

addRule()changeRule()explainAction()

AccountBusinessRules

Page 47: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006

Take care

In older versions of UML, the notation of refinement was the notation which is now used for realization

47

Page 48: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 48

Page 49: 05-ClassDiagrams

Roel Wuyts - ULB - Analyse et Méthodologie Informatiques - 2005/2006 49

Wrap-up

Classes have 3 parts: name, attributes, operations

They can have relationships