You are on page 1of 69

Rekayasa Perangkat Lunak

Desain dengan UML


(Unified Modeling
Language)
Oleh: Bayu Hendradjaya

Pengajar:
Bayu Hendradjaya/Christine Suryadi

UML:
Unified Modeling Language

The Unified Modeling Language (UML)


is a family of graphical notations
that help in describing and designing
software systems, particularly those
built using object-oriented (OO) style
[Martin Fowler in UML Distilled]

Desain dengan UML

Introduction to UML
UML: pictures of an OO system
programming languages are not abstract enough for
OO design
UML is an open standard; lots of companies use it

What is legal UML?


a descriptive language: rigid formal syntax (like
programming)
a prescriptive language: shaped by usage and
convention

it's okay to omit things from UML diagrams if


they aren't needed by
team/supervisor/instructor
Desain dengan UML

History
Design notation of various kinds used
since programming began:
Flow charts, Pseudo code, Structure Diagrams

With advent of OO, many conflicting


notations (1980s , 1990s)
These notations were combined and
unified (late 1990s)
UML extensively taught in universities
(2000 - now).

Desain dengan UML

Uses for UML


(Both forward & reverse engineering)

as a sketch: to communicate aspects of system


forward design: doing UML before coding
backward design: doing UML after coding as documentation
often done on whiteboard or paper
used to get rough selective ideas

as a blueprint: a complete design to be implemented


sometimes done with CASE (Computer-Aided Software
Engineering) tools

as a programming language: with the right tools,


code can be auto-generated and executed from UML
only good if this is faster than coding in a "real" language
Desain dengan UML

Diagram
UML

Structured
Diagram
Class Diagram
Component Diagram
Composite Structure
Diagram
Deployment Diagram
Object Diagram
Package Diagram

Desain dengan UML

Behavior
Diagram
Use Case Diagram
Activity Diagram
State Machine Diagram
Interaction Diagram
Sequence Diagram
Communication Diagram
Interaction Overview
Diagram
Timing Diagram
6

Class Diagrams

UML class diagrams


What is a UML class diagram?
UML class diagram: a picture of the classes in an OO
system, their fields and methods, and connections
between the classes that interact or inherit from each
other

What are some things that are not represented in a


UML class diagram?

details of how the classes interact with each other


algorithmic details; how a particular behavior is
implemented
Desain dengan UML

Example UML Class Diagram

http://www.agiledata.org/images/oo101ClassDi
Desain
agram.gif

dengan UML

Diagram of one class


class name in top of box
write <<interface>> on top of interfaces' names
use italics for an abstract class name

attributes (optional)
should include all fields of the object

operations / methods (optional)


may omit trivial (get/set) methods
but don't omit any methods from an interface!

should not include inherited methods

Desain dengan UML

10

Class attributes

attributes (fields, instance variables)


visibility name : type [count] = default_value
visibility:

+ public
# protected
private
~ package (default)
/
derived
underline static attributes
derived attribute: not stored, but can
be computed from other attribute values
attribute example:
- balance : double = 0.00

Desain dengan UML

11

11

Class operations / methods


operations / methods
visibility name (parameters) : return_type
visibility:

+ public
# protected
- private
~ package (default)
underline static methods
parameter types listed as (name: type)
omit return_type on constructors and
when return type is void
method example:
+ distance(p1: Point, p2: Point): double
Desain dengan UML

12

12

Instance Specification
(Object)

ObjectName: Class Name


Attribute : type
Operation (arg list) : return type
Abstract operation

Various parts are optional


Desain dengan UML

13

Comments
represented as a folded note, attached to
the appropriate class/method/etc by a
dashed line

Desain dengan UML

14

Associational
relationships
associational (usage) relationships
1. multiplicity (how many are used)

*
1
2..4
3..*

0, 1, or more
1 exactly
between 2 and 4, inclusive
3 or more

2. name (what relationship the objects have)


3. navigability (direction)

Desain dengan UML

15

Multiplicity of
associations
one-to-one

each student must carry exactly one ID card

one-to-many

one rectangle list can contain many rectangles

Desain dengan UML

16

Association
types

Car

1
1

aggregation: "is part of"


symbolized by a clear white diamond

Engine

composition: "is entirely made of"


stronger version of aggregation
the parts live and die with the whole
symbolized by a black diamond

aggregation

Book

composition

1
*
Page

dependency: "uses temporarily"


symbolized by dotted line
often is an implementation
detail, not an intrinsic part of
that object's state
Lottery
Desain dengan UML

Ticket

dependency

Random
17

Generalization
(Inheritance)

Supertype

Subtype 1

Subtype 2

Generalization: an inheritance
relationship
inheritance between classes
interface implementation
Desain dengan UML

18

Generalization Relationships
Generalization (inheritance)
relationships
hierarchies drawn top-down with
arrows pointing upward to parent
line/arrow styles differ, based on
whether parent is a(n):
class:
solid line, black arrow
abstract class:
solid line, white arrow
interface:
dashed line, white arrow
we often don't draw trivial / obvious
generalization relationships, such as
drawing the Object
class as a parent
Desain dengan UML

19

Class diagram example 1

Desain dengan UML

20

Class diagram example 2


Multiplicity
Customer
Class

Simple
Aggregation

Rental Invoice

Abstract
Class
Rental Item

1..*

0..1

Composition

Simple
Association

Generalization

DVD Movie

VHS Movie

Video Game

Desain dengan UML

Checkout Screen

21

Class diagram example 3

StudentBody

+ main (args :
String[])

Address

Student

100 - firstName : String


- lastName : String
- homeAddress :
Address
- schoolAddress :
Address
+ toString() : String

- streetAddress :
String
- city : String
- state : String
- zipCode : long
+ toString() : String
Desain dengan UML

22

Sequence Diagrams

UML sequence diagrams


Sequence diagram: an "interaction
diagram" that models a single
scenario executing in the system
perhaps 2nd most used UML diagram
(behind class diagram)

relation of UML diagrams to other


exercises:
CRC cards-> class diagram
use cases -> sequence diagrams
Desain dengan UML

24

Key parts of a sequence diag.


Participant: an object or entity that acts
in the sequence diagram
sequence diagram starts with an unattached
"found message" arrow

Message: communication between


participant objects
The axes in a sequence diagram:
horizontal: which object/participant is acting
vertical: time (down -> forward in time)
Desain dengan UML

25

Elements of Sequence Diagrams


Name: Class
Create
New object

Message

Self-call

Return
Delete

There is also notation for loops, conditions,


Desain dengan UML
etc.

26

Sequence Diagram from use case

Desain dengan UML

27

Representing objects
Squares with object type, optionally
preceded by object name and colon
write object's name if it clarifies the diagram
object's "life line" represented by dashed vert.
line

Desain dengan UML

28

Messages between objects


Message (method call) indicated by
horizontal arrow to other object
write message name and arguments above
arrow

Desain dengan UML

29

Messages, continued
message (method call) indicated by
horizontal arrow to other object
dashed arrow back indicates return
different arrowheads for normal / concurrent
(asynchronous) methods

Desain dengan UML

30

Lifetime of objects
creation: arrow with
'new' written above it
notice that an object
created after the start of
the scenario appears lower
than the others

deletion: an X at bottom
of object's lifeline
Java doesn't explicitly
delete objects; they fall out
of scope and are garbagecollected
Desain dengan UML

31

Indicating method calls


activation: thick box over object's life line;
drawn when object's method is on the stack
either that object is running its code, or it is on
the stack waiting for another object's method
to finish
nest to indicate recursion
Activation

Nesting

Desain dengan UML

32

Indicating selection and loops

frame: box around part of a sequence diagram to indicate


selection or loop
if
if/else
dashed line
loop

Desain dengan UML

-> (opt) [condition]


-> (alt) [condition], separated by horizontal
-> (loop) [condition or items to loop over]

33

Linking sequence diagrams

if one sequence diagram is too large or refers to another


diagram, indicate it with either:
an unfinished arrow and comment
a "ref" frame that names the other diagram
when would this occur in our system?

Desain dengan UML

34

Example sequence diagram

Desain dengan UML

35

Forms of system control


What can you say about the control flow of each
of the following systems?
Is it centralized?
Is it distributed?

Desain dengan UML

36

Flawed sequence diagram 1

What's wrong with this sequence diagram? (Look at the


UML syntax and the viability of the scenario.)

Desain dengan UML

37

Flawed sequence diagram 2


What's
wrong with
this
sequence
diagram?

Desain dengan UML

38

Why not just code it?


Sequence diagrams can be somewhat
close to the code level. So why not just
code up that algorithm rather than
drawing
it
as
a
sequence
diagram?

a good sequence diagram is still a bit above


the level of the real code (not all code is drawn
on diagram)
sequence diagrams are language-agnostic (can
be implemented in many different languages
non-coders can do sequence diagrams
easier to do sequence diagrams as a team
can see many objects/classes at a time on
same page (visual bandwidth)

Desain dengan UML

39

Example for Sequence Diagram


[Fowler]

We have an order and are going to


invoke a command on it to calculate
its price. To do that, the order needs
to look at all the line items on the
order and determine their prices,
which are based on the pricing rules
of the order lines products. Having
done that for all the line items, the
order then needs to compute an
overall discount, which is based on
rules tied to the customer.
Desain dengan UML

40

Example for Sequence Diagram


[Fowler]

We have an order and are going to


invoke a command on it to calculate
its price. To do that, the order needs
to look at all the line items on the
order and determine their prices,
which are based on the pricing rules
of the order lines products. Having
done that for all the line items, the
order then needs to compute an
overall discount, which is based on
rules tied to the customer.
Desain dengan UML

41

Example Sequence Diagram


anOrder
calculatePrice

anOrderLin
e

getQuantity
getProduct

Found
message

aCustom
er

aProduct

aProduct

getPricingDetails

getBasePrice

Participan
t
Return

Lifeline
Activati
on

Self call
Messag
e

calculateDiscounts
getDiscountInfo

Desain dengan UML

42

Object Diagram

Desain dengan UML

43

Object Diagram
An object diagram is a snapshot of the
objects in a system at a point in time.
Because it shows instances rather than classes,
an object diagram is often called an instance
diagram.

When to use?
Object diagrams are useful for showing examples
of objects connected together.
In many situations, you can define a structure precisely
with a class diagram, but the structure is still difficult
to understand.
In these situations, a couple of object diagram
examples can make all the difference
Desain dengan UML

44

Contoh Kelas Account dan Objeknya

Desain dengan UML

45

Class Diagram

Object Diagram

Desain dengan UML

46

Package Diagram

Desain dengan UML

47

Package Diagram
A package is a grouping construct
that allows you to take any construct
in the UML and group its elements
together into higher-level units.
Its most common use is to group classes
In programming terms, packages
correspond to such grouping constructs
as packages (in Java) and namespaces
(in C++ and .NET).

Desain dengan UML

48

Ways of Showing Packages

Desain dengan UML

49

Dependencies in Package

Desain dengan UML

50

Deployment Diagram

Desain dengan UML

51

Deployment Diagram
Deployment diagrams show a
system's physical layout, revealing
which pieces of software run on what
pieces of hardware.

Desain dengan UML

52

Desain dengan UML

53

Activity Diagram

Desain dengan UML

54

Activity Diagram
Activity diagrams are a technique to
describe procedural logic, business
process, and work flow.
In many ways, they play a role similar to
flowcharts, but the principal difference
between them and flowchart notation is
that they support parallel behavior.

Desain dengan UML

55

Example of Activity Diagram

Desain dengan UML

56

Decomposing An Action
Actions can be decomposed
into subactivities

Desain dengan UML

57

Subsidiary
Activity Diagram

Desain dengan UML

58

Partitions
Activity diagrams tell
you what happens,
but they do not tell
you who does what

Desain dengan UML

59

Activity Diagram use

Desain dengan UML

60

State Machine Diagram

Desain dengan UML

61

State Machine Diagram

Desain dengan UML

62

Software Lifecycle:
UML Can Be Used at Various Stages

Analysis: OOA
What is system to do? What services is it
to provide? Provide requirements for
designer.

Design: OOD
What will be the parts & structure of the
system? How will they interact? Provide
blueprint for programmer.

Programming: OOP
Desain dengan UML

63

Putting UML into Processes


Requirement Analysis
Use cases, which describe how people interact with
the system.
A class diagram drawn from the conceptual
perspective, which can be a good way of building up
a rigorous vocabulary of the domain.
An activity diagram, which can show the work flow
of the organization, showing how software and
human activities interact. An activity diagram can
show the context for use cases and also the details
of how a complicated use case works.
A state diagram, which can be useful if a concept
has an interesting life cycle, with various states and
events that change that state.
Desain dengan UML

64

Putting UML into Processes (2)


Desain
Class diagrams from a software perspective. These
show the classes in the software and how they
interrelate.
Sequence diagrams for common scenarios. A
valuable approach is to pick the most important and
interesting scenarios from the use cases and use CRC
cards or sequence diagrams to figure out what happens
in the software.
Package diagrams to show the large-scale
organization of the software.
State diagrams for classes with complex life histories.
Deployment diagrams to show the physical layout of
the software.
Desain dengan UML

65

Design phase
Design: specifying the structure of how a
software system will be written and function,
without actually writing the complete
implementation
A transition from "what" the system must do,
to "how" the system will do it
What classes will we need to implement a system
that meets our requirements?
What fields and methods will each class have?
How will the classes interact with each other?
Desain dengan UML

66

How do we design
classes?
class identification from project spec / requirements
nouns are potential classes, objects, fields
verbs are potential methods or responsibilities of a class

CRC card exercises


write down classes' names on index cards
next to each class, list the following:
responsibilities: problems to be solved; short verb phrases
collaborators: other classes that are sent messages by this
class (asymmetric)

UML diagrams
class diagrams (today)
sequence diagrams
...
Desain dengan UML

67

67

Tugas
Untuk topik masing-masing:
Buat SKPL untuk pendekatan
berorientasi objek (template ada di
situs) yang mencakup:
Diagram use case dan skenario-nya
Untuk setiap use case:
Buat diagram kelas
Buat diagram activity (menggambarkan
seluruh alternatif skenario)

Dikumpulkan: 16 November 2015


(jam kuliah)
Desain dengan UML

68

Closing
Slide Acknowledgement
Introduction to UML: Unified Modeling Language, Slide
Presentation of Ric Holt, U. Waterloo, March 2009 CS246
Design and UML Class Diagrams Slide presentation of
Marty Stepp, U. Washington, 2007.

TextBook:
Martin Fowler, UML Distilled: a brief guide to the standard object
modeling language. 3rd edition Addison-Wesley Professional, 2004.
Scott W. Ambler, The Object Primer: Agile Model Driven
Development with UML 2. 3rd edition Cambridge University Press,
2004.

Desain dengan UML

69

You might also like