Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Chapter 16

UML Class Diagrams


Larman, C. “Applying UML and
Patterns”. 3rd Ed.
Ed. Prentice-Hall: 2005.
Fig. 16.1
officially in UML, the top format is
SuperclassFoo used to distinguish the package
or name from the class name
SuperClassFoo { abstract }
unofficially, the second alternative
- classOrStaticAttribute : Int is common
+ publicAttribute : String
3 common
- privateAttribute
compartments
assumedPrivateAttribute
isInitializedAttribute : Bool = true java.awt::Font
1. classifier name
aCollection : VeggieBurger [ * ] or
attributeMayLegallyBeNull : String [0..1] java.awt.Font
2. attributes
finalConstantAttribute : Int = 5 { readOnly }
/derivedAttribute plain : Int = 0 { readOnly }
3. operations
bold : Int = 1 { readOnly }
+ classOrStaticMethod() name : String
+ publicMethod() style : Int = 0
assumedPublicMethod() ...
an interface - privateMethod()
shown with a # protectedMethod() getFont(name : String) : Font
keyword ~ packageVisibleMethod() getName() : String
«constructor» SuperclassFoo( Long ) ...
methodWithParms(parm1 : String, parm2 : Float)
methodReturnsSomething() : VeggieBurger
«interface» methodThrowsException() {exception IOException}
Runnable abstractMethod()
abstractMethod2() { abstract } // alternate Fruit
run() finalMethod() { leaf } // no override in subclass dependency
synchronizedMethod() { guarded } ...

...

interface
implementation
and
subclassing SubclassFoo
PurchaseOrder
... 1
...
run() order
...
...
association with
multiplicities
- ellipsis “…” means there may be elements, but not shown
- a blank compartment officially means “unknown” but as a
convention will be used to mean “no members”
Fig. 16.2

Domain Model Sale


Register Captures 1
1
time
conceptual
... isComplete : Boolean
perspective
/total

Register Sale
Design Model ... time
1
isComplete : Boolean
DCD; software endSale() currentSale /total
perspective enterItem(...)
makePayment(...) makeLineItem(...)
Fig. 16.3

Register Sale
using the attribute
text notation to ...
currentSale : Sale
indicate Register has
a reference to one ... ...
Sale instance

Register Sale
OBSERVE: this style
visually emphasizes 1
... ...
the connection
currentSale
between these classes ... ...

using the association notation to indicate


Register has a reference to one Sale instance

Register Sale
thorough and
unambiguous, but some 1
currentSale : Sale ...
people dislike the
currentSale
possible redundancy ... ...
Fig. 16.4

the association name, common when drawing a


domain model, is often excluded (though still legal)
when using class diagrams for a software
perspective in a DCD

Register Sale
UP Domain Model 1 Captures-current-sale 1
conceptual perspective id : Int time : DateTime

UP Design Model Register Sale


DCD 1
id: Int time: DateTime
software perspective currentSale
... ...
Fig. 16.5

Register Sale
applying the guideline
to show attributes as 1
id: Int time: DateTime
attribute text versus as currentSale
association lines ... ...

Register has THREE attributes: Store


1. id 1
2. currentSale address: Address
3. location phone: PhoneNumber
location
...
Fig. 16.6

Sale SalesLineItem

time: DateTime ...


lineItems : SalesLineItem [1..*]
or ...
lineItems : SalesLineItem [1..*] {ordered}

...
Two ways to show a
collection attribute

Sale SalesLineItem
1..*
time: DateTime ...
lineItems
... {ordered, List} ...

notice that an association end can optionally also


have a property string such as {ordered, List}
Fig. 16.7

Register
«method»
// pseudo-code or a specific language is OK ...
public void enterItem( id, qty )
{ endSale()
ProductDescription desc = catalog.getProductDescription(id); enterItem(id, qty)
sale.makeLineItem(desc, qty); makeNewSale()
} makePayment(cashTendered)
Fig. 16.8

declaring the stereotype using the stereotype

UML extension a tool will probably allow a popup to fill in the tag values,
relationship to a basic once an element has been stereotyped with «authorship»
UML metamodel term –
Element

«authorship»
«metaclass» «stereotype» «authorship»
author = “craig”
Element Authorship Square
status = “tested”
... author: String ...
status : String
Fig. 16.9

the Sale has parameter visibility to a


ProductDescription, and thus some kind of
dependency
ProductDescription

...
Sale
...
...

updatePriceFor( ProductDescription )
... SalesLineItem

1..* ...
lineItems
...
Fig. 16.10

the doX method invokes the runFinalization


static method, and thus has a dependency on
the System class
System

...
Foo
runFinalization()
... ...

doX()
...
Fig. 16.11

Clock B
Window «call» A «create»
getTime() ...
...

a dependency on calling on operations of a dependency that A objects create B objects


the operations of a Clock
Fig. 16.12
socket line notation

Timer Window1 uses the Timer dependency line notation


Window1
interface
Window2 has a dependency on the
it has a required interface Timer interface when it collaborates
with a Clock2 object

«interface» Clock2
Timer
... Timer Window2
getTime()
getTime()
...

Clock1
implements and
provides the Clock3
Timer interface
Clock1 ... Timer Window3
... getTime()
...
getTime() socket line notation
... lollipop notation indicates Clock3 implements
and provides the Timer interface to clients Window3 has a dependency on the
Timer interface when it collaborates
Timer is a provided interface with a Clock3 object
Fig. 16.13

1 0..7 composition means


Hand Finger
-a part instance (Square) can only be part of one
composite (Board) at a time

composition -the composite has sole responsibility for management of


its parts, especially creation and deletion

1 40 1 1..*
Board Square Sale SalesLineItem
Fig. 16.14

three ways to show UML constraints

Stack

size : Integer { size >= 0 }

push( element ) { post condition: new size = old size + 1 }


pop() : Object

{
post condition: new size = old size – 1
}
Fig. 16.15

(a) Product Contains Product


Catalog Description
1 1..*

1 1
Product Contains Product
(b) itemID
Catalog Description

qualifier multiplicity reduced to 1


Fig. 16.16

Employs
Company * * Person

a person may have Employment


employment with several
companies salary
startDate
Fig. 16.17

1
ServicesFactory UML notation: this '1'
can optionally be used
UML notation: in a instance : ServicesFactory to indicate that only one
class box, an instance will be created
underlined attribute or accountingAdapter : IAccountingAdapter (a singleton)
method indicates a inventoryAdapter : IInventoryAdapter
static (class level) taxCalculatorAdapter : ITaxCalculatorAdapter
member, rather than
an instance member getInstance() : ServicesFactory

getAccountingAdapter() : IAccountingAdapter
getInventoryAdapter() : IInventoryAdapter
getTaxCalculatorAdapter() : ITaxCalculatorAdapter
...
Fig. 16.18

the attribute type may be expressed in


parameterized or template K official UML, with the template binding
interfaces and classes «interface» syntax requiring an arrow
List or
K is a template parameter in another language, such as Java
clear()
...

Board

squares : List<K Square>


anonymous class with or
template binding complete T squares : List<Square>
ArrayList ...
elements : T[*]
ArrayList<T Square> ...
clear() clear()
... for example, the elements attribute is an
...
array of type T, parameterized and bound
before actual use.
there is a chance the UML 2 “arrow” symbol will
eventually be replaced with something else e.g., ‘=’
Fig. 16.19

DataAccessObject

id : Int
...

doX()
...

exceptions thrown
DatabaseException
IOException

responsibilities
serialize and write objects
read and deserialize objects
...
Fig. 16.20

active class

Clock
«interface»
Runnable ...
run() run()
...
Fig. 16.21

: Register : Sale

makePayment(cashTendered)

makePayment(cashTendered)

messages in interaction
diagrams indicate operations
in the class diagrams classes
identified in the
interaction
diagrams are
Register Sale declared in the
1 class diagrams
... ...
currentSale
makePayment(…) makePayment(…)
... ...

You might also like