Class diagram
A class diagram depicts classes and their interrelationships
Used for describing structure and behavior in the use cases
Provide a conceptual model of the system in terms of
entities and their relationships
Used for requirement capture, end-user interaction
Detailed class diagrams are used for developers
Class diagram
Each class is represented by a rectangle subdivided into three
compartments
Name
Attributes
Operations
Modifiers are used to indicate visibility of attributes and
operations.
‘+’ is used to denote Public visibility (everyone)
‘#’ is used to denote Protected visibility (friends and derived)
‘-’ is used to denote Private visibility (no one)
By default, attributes are hidden and operations are visible.
Class diagram
Name
Account_Name
- Customer_Name
Attributes
- Balance
+addFunds( ) Operations
+withDraw( )
+transfer( )
OO Relationships
There are two kinds of Relationships
Generalization (parent-child relationship)
Association (student enrolls in course)
Associations can be further classified as
Aggregation
Composition
OO Relationships: Generalization
Supertype Example: Customer
Regular Loyalty
Customer Customer
Subtype1 Subtype2
-Inheritance is a required feature of object orientation
-Generalization expresses a parent/child relationship among related classes.
-Used for abstracting details in several layers
A generalisation hierarchy
Employee
Ma nager Programmer
budgetsControlled project
progLanguage
dateAppointed
Project De pt. Strategic
Ma nag er Ma nager Ma nag er
projects dept responsibilities
6
OO Relationships: Association
Represent relationship between instances of classes
Student enrolls in a course
Courses have students
Courses have exams
Etc.
Association has two ends
Role names (e.g. enrolls)
Multiplicity (e.g. One course can have many students)
Navigability (unidirectional, bidirectional)
Association: Multiplicity and Roles
student
1 *
University Person
0..1 *
employer teacher
Multiplicity Role
Symbol Meaning
1 One and only one Role
0..1 Zero or one “A given university groups many people;
M..N From M to N (natural language)
some act as students, others as teachers.
A given student belongs to a single
* From zero to any positive integer university; a given teacher may or may not
0..* From zero to any positive integer be working for the university at a particular
1..* From one to any positive integer time.”
Objects Association
Modeling an association between two
classes means that there is some sort of
relationship between objects of each
class that may be connected.
studies
Student Course
0..* 1..*
9
Association: Model to Implementation
* 4
Student Course
has enrolls
Class Student {
Course enrolls[4];
}
Class Course {
Student have[];
}
OO Relationships: Composition
Whole Class
Class W Association
Models the part–whole relationship
Composition
Class P1 Class P2 Also models the part–whole relationship but, in
addition, Every part may belong to only one
whole, and If the whole is deleted, so are the
Part Classes parts
[From Dr.David A. Workman]
Example Example:
A number of different chess boards: Each square
belongs to only one board. If a chess board is
thrown away, all 64 squares on that board go as well.
Figure 16.7
The McGraw-Hill Companies, 2005
OO Relationships: Aggregation
Container Class
Aggregation:
Class C expresses a relationship among instances of related
classes. It is a specific kind of Container-
AGGREGATION
Containee relationship.
Class E1 Class E2 express a more informal relationship than
composition expresses.
Containee Classes Aggregation is appropriate when Container and
Containees have no special access privileges to
each other.
Example
Bag
Apples Milk
[From Dr.David A. Workman]
Object aggregation
Study pack
Course titl e
Number
Year
Instructor
1 1 1 1
1 ..* 1 ..* 1 ..* 0 ..*
Assignment sli des Lecture Vi deotape
notes
Credi ts Slides Tape ids.
Text
1 1
1 ..* 1 ..*
Exercises Soluti ons
#Problems Text
Description Diagrams
13
Aggregation vs. Composition
Composition is really a strong form of association
components have only one owner
components cannot exist independent of their owner
components live or die with their owner
e.g. Each car has an engine that can not be shared with other cars.
Aggregations
may form "part of" the association, but may not be essential to it. They
may also exist independent of the aggregate. e.g. Apples may exist
independent of the bag.
Class Modeling
Identify objects and classes
Prepare a data dictionary
Identify associations between objects
Identify class attributes and initial set of
operations
Organize object classes using inheritance
Classes, Attributes and Operations
Attributes define the properties of the objects
every instance of the class has the same attributes
an attribute has a data type
the values of the attributes may differ among instances
Operations define the behavior of the objects
action performed on or by an object
available for all instances of the class
need not be unique among classes
Class Attributes Operations
ball radius, weight catch, throw
football air pressure pass, kick, hand-off
baseball liveness hit, pitch, tag
Instantiation - Example
Person (Person)
name Joe Smith
age age=39
weight weight=158
(Person)
Mary Wilson
age=27
weight=121
Inheritance
Classes with a set of similar attributes and operations may
be organized into a hierarchical relationship
Common attributes and operations are factored out and
assigned to a broad superclass (generalization)
generalization is the “is-a” relationship
superclasses are ancestors, subclasses are descendants
A class can be iteratively refined into subclasses that inherit
the attributes and operations of the superclass
(specialization)
OMT Inheritance
Notation
Generalization
Superclass
Class Ball
Attributes Radius, Weight
Operations Throw, Catch
Subclasses
Football Baseball Basketball
air pressure liveness air pressure , dimples
pass, kick, hand-off hit, pitch, tag shoot, dribble, pass
Specialization
Association and Links
An association is a relation among two or more classes
describing a group of links, with common structure and
semantics
A link is a relationship or connection between objects and is
an instance of an association
A link or association is inherently bi-directional
the name may imply a direction, but it can usually be inverted
the diagram is usually drawn to read the link or association from left
to right or top to bottom
A role is one end of an association
roles may have names
OMT Association Notation
Class, Association, and Roles
Person Company
Works For
equivalent
Company Person
Employs
Employer Employee
Object and Link
(Person) (Company)
Works For
Johnson IBM
Association and Links
has-capital Class diagram
Country City
nam nam
e e
(Country) has-capital (City)
Canada Ottawa
(Country) has-capital (City) Instance diagram
France Paris
(Country) has-capital (City)
Austria Vienna
Multiplicity of Associations
Multiplicity is the number of instances of one
class that may relate to a single instance of
an associated class
1-to-1
1-to-many (0 or more) 1+
1-to-(zero-or-one) ‘optional’ n
1-to-(one-or-more) ‘required’
1-to-n
OMT Multiplicity Notation
Instructor Courses
1+ Teaches
Takes
6-65
Student
Each course has at least one instructor
and between 6 and 65 students
A student may take many courses
An instructor may teach many courses
Class Model
employee order order DB
name number
password account
total cost
error message receipt item
explanation order number name
ship date quantity
total cost price
Class Model, continued
Since both receipts and error messages will be generated as output
it might make sense to have them as subclasses of a more general
class. We do not know enough yet to assign it attributes however.
response
error message receipt
explanation order number
ship date
total cost
Class Model, relationships
employee order order DB
name
number
password
account
total cost
1+
error message receipt item
explanation order number name
ship date quantity
total cost price
Scenario
A bank system contains data on customers
(identified by name and address) and their
accounts.
Each account has a balance and there are 2
type of accounts: one for savings which offers
an interest rate, the other for investments, used
to buy stocks.
Stocks are bought at a certain quantity for a
certain price (ticker) and the bank applies
commission on stock orders
Class Diagram