Você está na página 1de 70

IF2250 Dasar Rekayasa Perangkat Lunak

Desain dengan UML


(Unified Modeling Language)
Oleh: Bayu Hendradjaya

Pengajar:
Bayu Hendradjaya/Christine Suryadi/Yani Widyani
Slide kuliah bisa di download dari
kuliah.itb.ac.id /app245(subyek IF2250)

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]

IF2250- Desain dengan UML/Bayu Hendradjaya

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
IF2250- Desain dengan UML/Bayu Hendradjaya

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).

IF2250- Desain dengan UML/Bayu Hendradjaya

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
IF2250- Desain dengan UML/Bayu Hendradjaya

Diagram
UML

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

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

IF2250- Desain dengan UML/Bayu Hendradjaya

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

IF2250- Desain dengan UML/Bayu Hendradjaya

Example UML Class Diagram

http://www.agiledata.org/images/oo101ClassDiagram.gif
IF2250- Desain dengan

UML/Bayu Hendradjaya

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

IF2250- Desain dengan UML/Bayu Hendradjaya

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
IF2250- Desain dengan UML/Bayu Hendradjaya

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
IF2250- Desain dengan UML/Bayu Hendradjaya

12

12

Instance Specification
(Object)

ObjectName: Class Name


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

Various parts are optional

IF2250- Desain dengan UML/Bayu Hendradjaya

13

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

IF2250- Desain dengan UML/Bayu Hendradjaya

14

Associational relationships
associational (usage) relationships
1. multiplicity

*
1
2..4
3..*

(how many are used)

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

2. name
3. navigability

(what relationship the objects have)


(direction)

IF2250- Desain dengan UML/Bayu Hendradjaya

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

IF2250- Desain dengan UML/Bayu Hendradjaya

16

Car

Association types

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

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
IF2250- Desain dengan UML/Bayu Hendradjaya

aggregation

dependency

Lottery
Ticket

Random
17

Generalization
(Inheritance)
Supertype

Subtype 1

Subtype 2

Generalization: an inheritance relationship


inheritance between classes
interface implementation
IF2250- Desain dengan UML/Bayu Hendradjaya

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
IF2250- Desain dengan UML/Bayu Hendradjaya

19

Class diagram example 1

IF2250- Desain dengan UML/Bayu Hendradjaya

20

Class diagram example 2


Multiplicity
Customer

Simple

Class

Aggregation

Rental Invoice

Abstract
Class
Rental Item

1..*
1

0..1

Composition

Simple

Generalization

Association

Checkout Screen
DVD Movie

VHS Movie

Video Game

IF2250- Desain dengan UML/Bayu Hendradjaya

21

Class diagram example 3


StudentBody

100

+ main (args : String[])

Address

Student

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

- streetAddress : String
- city : String
- state : String
- zipCode : long
+ toString() : String
IF2250- Desain dengan UML/Bayu Hendradjaya

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
use cases

-> class diagram


-> sequence diagrams

IF2250- Desain dengan UML/Bayu Hendradjaya

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)
IF2250- Desain dengan UML/Bayu Hendradjaya

25

Elements of Sequence Diagrams


Name: Class

Create
New object

Message

Self-call

Return
Delete

There is also notation for loops, conditions, etc.

IF2250- Desain dengan UML/Bayu Hendradjaya

26

Sequence Diagram from use case

IF2250- Desain dengan UML/Bayu Hendradjaya

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

IF2250- Desain dengan UML/Bayu Hendradjaya

28

Messages between objects


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

IF2250- Desain dengan UML/Bayu Hendradjaya

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

IF2250- Desain dengan UML/Bayu Hendradjaya

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 garbage-collected

IF2250- Desain dengan UML/Bayu Hendradjaya

31

I ndicating 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

I F2250- Desain dengan UML/Bayu Hendradjaya

32

I ndicating selection and loops


frame: box around part of a sequence diagram to indicate selection or loop
if
-> (opt) [condition]
if/else -> (alt) [condition], separated by horizontal dashed line
loop
-> (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?

I F2250- Desain dengan UML/Bayu Hendradjaya

34

Example sequence diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

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?

36

Flawed sequence diagram 1


What's wrong with this sequence diagram? (Look at the UML syntax and
the viability of the scenario.)

I F2250- Desain dengan UML/Bayu Hendradjaya

37

Flawed sequence diagram 2


What's wrong
with this
sequence
diagram?

I F2250- Desain dengan UML/Bayu Hendradjaya

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)

I F2250- Desain dengan UML/Bayu Hendradjaya

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.
I F2250- Desain dengan UML/Bayu Hendradjaya

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.
I F2250- Desain dengan UML/Bayu Hendradjaya

41

Example Sequence Diagram


anOrderLine

anOrder
calculatePrice

getQuantity
getProduct

Found

aCustomer

aProduct

Participant

Lifeline

aProduct

message

Activation
getPricingDetails

getBasePrice

Return

Self call
Message

calculateDiscounts
getDiscountInfo

I F2250- Desain dengan UML/Bayu Hendradjaya

42

Object Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

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
I F2250- Desain dengan UML/Bayu Hendradjaya

44

Contoh Objek
Nama Objek

Nama Kelas

Novel ku : Buku
I sbn
Judul
Pener bi t
Juml ahHal aman

:
:
:
:

Nama at ribut

st r i ng[ 10] = 3432- 353840


st r i ng[ 30] = Kumpul an Pui si
st r i ng[ 20] = Pener bi t I TB
i nt = 135

T ipe at ribut

I F2250- Desain dengan UML/Bayu Hendradjaya

Nilai At ribut

45

Contoh Kelas Account dan Objeknya


Buku
I sbn : st r i ng[ 10]
Judul : st r i ng[ 30]
Pener bi t : st r i ng
Juml ahHal aman: i nt
Updat eJudul ( )
Updat ePener bi t ( )
Pendaf t ar anBukuBar u( )
PenghapusanBuku( )

<<inst ant iat e>>

<<inst ant iat e>>

Novel ku : Buku

Komi k : Buku

I sbn = 3432- 353840


Judul = Kumpul an Pui si
Pener bi t = Pener bi t I TB
Juml ahHal aman= 135

I sbn = 3432- 3840


Judul = Bat man
Pener bi t = Pener bi t I TB
Juml ahHal aman= 55

I F2250- Desain dengan UML/Bayu Hendradjaya

46

Class Diagram

Object Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

47

Package Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

48

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).

I F2250- Desain dengan UML/Bayu Hendradjaya

49

Ways of Showing Packages

I F2250- Desain dengan UML/Bayu Hendradjaya

50

Dependencies in Package

I F2250- Desain dengan UML/Bayu Hendradjaya

51

Deployment Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

52

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

I F2250- Desain dengan UML/Bayu Hendradjaya

53

I F2250- Desain dengan UML/Bayu Hendradjaya

54

Activity Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

55

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.

I F2250- Desain dengan UML/Bayu Hendradjaya

56

Example of
Activity Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

57

Decomposing An Action
Actions can be decomposed into
subactivities

I F2250- Desain dengan UML/Bayu Hendradjaya

58

Subsidiary Activity
Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

59

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

I F2250- Desain dengan UML/Bayu Hendradjaya

60

Activity
Diagram use

I F2250- Desain dengan UML/Bayu Hendradjaya

61

State Machine Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

62

State Machine Diagram

I F2250- Desain dengan UML/Bayu Hendradjaya

63

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
I F2250- Desain dengan UML/Bayu Hendradjaya

64

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.
I F2250- Desain dengan UML/Bayu Hendradjaya

65

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.
I F2250- Desain dengan UML/Bayu Hendradjaya

66

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?

I F2250- Desain dengan UML/Bayu Hendradjaya

67

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
... I F2250- Desain dengan UML/Bayu Hendradjaya

68

68

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: 14 April 2015 (jam kuliah)


I F2250- Desain dengan UML/Bayu Hendradjaya

69

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
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.

I F2250- use-case

70

Você também pode gostar