week-7
week-7
1
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
...
2
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
3
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
4
Class attributes
attributes (fields, instance variables)
visibility name : type [count] = default_value
visibility: + public
# protected
- private
/ derived
underline static attributes
attribute example:
- balance : double = 0.00
5
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
6
Visibility of Elelments
7
Comments
represented as a folded note, attached to the
appropriate class/method/etc by a dashed line
8
Relationships btwn. classes
generalization: an inheritance relationship
inheritance between classes
interface implementation
9
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, Italic Format
interface:
dashed line, white arrow
10
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)
11
Multiplicity of associations
one-to-one
each student must carry exactly one ID card
one-to-many
one rectangle list can contain many rectangles
12
Car
Association types
1
aggregation
aggregation: "is part of" 1
Engine
symbolized by a clear white diamond
14
Class diagram example 2
Multiplicity
Customer Simple
1
Class Aggregation
Composition Simple
Generalization (subyping)
Association
Checkout Screen
DVD Movie VHS Movie Video Game
15
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
16
Tools for creating UML diags.
Violet (free)
http://horstmann.com/violet/
Rational Rose
http://www.rational.com/
17