Design and UML Class Diagrams Reading:: UML Distilled Ch. 3, by M. Fowler
Design and UML Class Diagrams Reading:: UML Distilled Ch. 3, by M. Fowler
Design and UML Class Diagrams Reading:: UML Distilled Ch. 3, by M. Fowler
Reading:
UML Distilled Ch. 3, by M. Fowler
These lecture slides are copyright (C) Marty Stepp, 2007. They may not be rehosted, sold, or
modified without expressed permission from the author. All rights reserved.
1
Big questions
What is UML?
Why should I bother? Do people really use UML?
2
Design phase
design: specifying the structure of how a
software system will be written and function,
without actually writing the complete
implementation
3
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
UML diagrams
class diagrams (today)
sequence diagrams
...
4
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
5
Uses for UML
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
7
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
8
Class attributes
attributes (fields, instance variables)
visibility name : type [count] = default_value
visibility:+ public
# protected
- private
~ package (default)
/ derived
underline static attributes
attribute example:
- balance : double = 0.00
9
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
10
Comments
represented as a folded note, attached to the
appropriate class/method/etc by a dashed line
11
Relationships btwn. classes
generalization: an inheritance relationship
inheritance between classes
interface implementation
12
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:
13
Associational relationships
associational (usage) relationships
1. multiplicity (how many are used)
* 0, 1, or more
1 1 exactly
2..4 between 2 and 4, inclusive
3..* 3 or more
2. name (what relationship the objects have)
3. navigability (direction)
14
Multiplicity of associations
one-to-one
each student must carry exactly one ID card
one-to-many
one rectangle list can contain many rectangles
15
Car
Association types
1
aggregation
aggregation: "is part of" 1
Engine
symbolized by a clear white diamond
17
Class diagram example 2
Multiplicity
Customer Simple
1
Class Aggregation
Composition Simple
Generalization
Association
Checkout Screen
DVD Movie VHS Movie Video Game
18
Class diagram example 3
StudentBody Student
1 100
- firstName : String
+ main (args : String[]) - lastName : String
- homeAddress : Address
- schoolAddress : Address
+ toString() : String
Address
- streetAddress : String
- city : String
- state : String
- zipCode : long
+ toString() : String
19
Tools for creating UML diags.
Violet (free)
http://horstmann.com/violet/
Rational Rose
http://www.rational.com/
20
Class design exercise
Consider this Texas Hold 'em poker game system:
2 to 8 human or computer players
Each player has a name and stack of chips
Computer players have a difficulty setting: easy, medium, hard
Summary of each hand:
Dealer collects ante from appropriate players, shuffles the deck, and
deals each player a hand of 2 cards from the deck.
A betting round occurs, followed by dealing 3 shared cards from the
deck.
As shared cards are dealt, more betting rounds occur, where each
player can fold, check, or raise.
At the end of a round, if more than one player is remaining, players'
hands are compared, and the best hand wins the pot of all chips bet
so far.
What classes are in this system? What are their responsibilities?
Which classes collaborate?
Draw a class diagram for this system. Include relationships
between classes (generalization and associational). 21