Class & Object Diagrams
Software Design Methodology
2
UML Class Diagrams
The heart of the UMLDescribes the classes in the system and the relations among themSupports most OO concepts
associationsaggregationinheritance (including multiple)
3
Classes
Classes are denoted by rectangles.AttributesOperations Responsibilities Class
attribute: type
operationoperation(parameters)
responsibility
4
Classes
Attribute: A named property of a class that describes a range of values that instances of the property may hold.Operation: Implementation of a service that can be requested.Responsibility: A contract or an obligation of a class. As the model is refined the responsibilities are transformed into attributes and operations.
5
Object• Object - a concept, an abstraction, a thing
meaningful to the domain • Joe Smith Person• Lassie Dog• flight #713 Flight• the top window Window
• Object Class - a collection of all objects having the same set of features.
• Each object has a unique identity and is an instance of its object class.
6
Class Diagrams vs. Object Diagrams
Person
nameemployeeIDtitle
Smith:PersonJoe
p:Person
name=“Yahoo”employeeID=1212title=“VP of production”
7
Relationships
A relationship is a connection among things.Common relationships: Dependencies, Associations, Generalization and Aggregations.
8
DependencyA dependency states that a change in specification of one thing may effect another thing that uses it
CourseCourseSchedule
add(c:Course)
remove(c:course)
Event
Dean
Window
open()
close()
Student
grade
CalculateAverage(grade)
A class takes an object of another class as a parameter
A class accesses an object of another class
A class calls a class operation in another class
9
An association is structural relationship that specifies that objects of one thing are connected to objects of another.
Association
CountryName: stringPopulation: integerArea: Km2
CityName: stringPopulation: integer
Has-capital
Binary association
10
Association Name
Printed at the center of the lineDescribes the nature of the relationship (usually a verb)A direction triangle can be added
11
Association Multiplicity
Used when it is important to state how many objects may be connected across an instance of an association. When a number is stated at an end of an association it is specified that for each object at the class at the opposite end there must be that many objects at the near end.An * denotes many - which can be any number between zero and infinity.
12
Multiplicity Example
Office
Employee Department1..*
1..10
1
0..1
At least one employee in a departmentAn employee belongs to exactly one departmentAn employee has zero or one officeAn office is assigned to a number of one up to 10
employees
13
Multiplicity symbols (participation constraints)
LineName: string
PointName: stringX-Coordinate: integerY-Coordinate: integer
Intersects0,2..* *
Is Endpoint Of 2*
14
Association Classes
Denoted as a class attached to the associationSpecify properties of the associationDoes not belong to any of the connected class
CarReg. Num.ModelYear
Person
NameYear Purchased
* 1.. *Owner of
15
Association Attributes
Works-forPerson
Name: stringID: stringAddress: string
Company
Name: stringAddress: string
salary: floatjob: string
1..* *
submitStudent
NameIDFaculty
AssignmentNumberSubjectSubmission date
Grade
140 4
16
Association Role Names
Names may be added at each end of the associationProvide better understanding of the association meaningEspecially helpful in self-associated classes
Person
Manager
Worker
Manages
Companyemployee employer
*
1..0
*..1 *
17
Role Names, Constraints and Qualifiers
Ordering
Qualifier:an attribute that reduces the association’s multiplicity.
ownerUser Directory
contents
authorized usercontainer
1
***
*
0..1
{ordered}Window Screenvisible-on
* 1
Directory Filefile name0..1*
Role
18
Classes versus Objects
Class Diagram Object Diagram
Classes nameattributesoperationsresponsibility
Association namemultiplicityrole
Association Classes
Objects values forattributes
Link
Link Object
19
Class Diagram Example
holdsReader
NameIDAddress
CopyCodeShelf locationStatusLoan duration
Return date
0..1 *
BookCodeNameEditionAuthor
of
reserved1
*
*
*{ordered}
20
Object Diagram Example
357:ReaderName=“Joe”ID=357Address=“Jerusalem”
57168-1:CopyCode=57168-1Shelf location=681.3.06Status=“at loan”Loan duration=2 weeks
holds
Return date=16.11.00
UML:BookCode=57168Name=“The Unified Modeling
Language User Guide”Edition=1Author=“Booch, Rumbaugh,
Jacobson”
of
reserved
482:ReaderName:”Michel”ID=482Address=“Haifa”
936:ReaderName=“David”ID=936Address=“Haifa”
57168-2:CopyCode=57168-2Shelf location=681.3.06Status=“at loan”Loan duration=1 month
holds
Return date=24.11.00
of
21
Aggregation
“whole-part” relationship between classesAssemble a class from other classesCombined with “many” - assemble a class from
a couple of instances of that classMay be replaced by associations...
SentenceParagraph
Picture
Documenttext fontpages
*
*
*
22
Aggregation (whole-part; part-of; and)
Monitor
Microcomputer
System box Mouse Keyboard
RAM CPU Fan Chassis
aggregate; whole
constituents; parts
0..* 1 0..1 1
* 1 0..1 1
23
ConstraintA functional relationship between entities
(objects, classes, attributes, links, associations) of an object model.
Employeesalary
Windowlengthwidth
boss
{salary < boss.salary} {0.8 ≤length/width≤1.5}
Company Office Person{ordered}
Member-ofPerson Committee
Chair-of{subset}
0..1
*
**
* **
24
Constraint (Cont.)
Persongender:{female, male}
Portfolio
{self.wife.gender=female and self.husband.gender=male}
{secured}=Across the association communicated is encrypted.
BankAccount
Corporation{secured}
{xor}
0..1
wife
0..1
husband
25
Inheritance (Generalization)Denoted by a triangle - connects one superclass to many subclassesMultiple inheritance is allowed
a subclass may be connected to more than one superclassnot recommended - hard to understand, hard to implement
Four applicable standard constraints:CompleteIncompletedisjointoverlapping
26
Generalization (gen-spec; kind-of; or)Figure
colorcenter positionpen typeMoveRotateDisplay
0 Dimensional 1 DimensionalOrientationScale
2 DimensionalOrientationfill typefillScale
RadiusAngle
Arc
Display
Control pts
Spline
Display
Endpoints
Line
Display Diameter
Circle
Display
Endpoints
Polygon
Display
superclass
subclasses
27
Multiple Inheritance
CheckingAccount
BankAccount
InterestBearingItem InsurableItemAsset
RealEstate Security
SavingAccount Stock Bond
28
Abstract Class - a class that has no direct instances
Class
Concrete Class
subclass
Abstract Class
Non-leaf Class
Leaf Class
Instance has directinstances
superclass
has subclasses
superclass
has subclasses
1..*
1..*
1..*
1..*
29
Recursive Aggregation
Program
Block
Compoundstatement
Simplestatement
2..*1..*
30
Recursive Terms (Multiple Binary term use)
ConstantValue
ExpressionBinary Operator
VariableName
First OperandSecond Operand
**
11
Term
31
(x+y/2)/(x/3+y)di1:ExpressionBinary op=“/”
pl2:ExpressionBinary op=“+”
pl1:ExpressionBinary op=“+”
di3:ExpressionBinary op=“/”
di2:ExpressionBinary op=“/”
y:VariableName=“y”
two:ConstantValue=2
x:VariableName=“x”
three:ConstantValue=3
2nd
1st1st1st
1st
1st
2nd
2nd2nd
2nd
32
Recursive Terms(Unary &Binary & Single Term Use)
Second Operand
ConstantValue
Expression VariableName
First Operand 11
Term
Binary Unary
33
Abstract Operations
HourlyEmployee
hourly rateovertime ratecompute pay
SalariedEmployee
weekly rate
compute pay
ExemptEmployee
monthly rate
compute pay
Employee
year-to-date earningscompute pay {abstract}
The origin class defines the protocol: •semantics;•attribute types;•number and type of arguments for methods
34
Generalization as Extension and Restriction
Extension: addition of new features.(e.g. Person vs. Student)Restriction: constraining ancestor
attributes.(e.g. nullifying some attributes)
35
Overriding Operations
Tension between use of inheritance for abstraction vs. implementation reuse.Reasons for overriding:
Extension(e.g. Person has Report_Marriage; Employee has Report_Marriage that extends the code of Report_Marriage)Restriction(e.g. Subclass operation use some of superclasssub-operations)
36
Reasons for overriding (cont.)
Optimization e.g. IntegerSet has findMax; SortedIntegerSet has another implementation for findMax.ConvenienceLook for a similar class and use it as superclass.This is ad hoc use which is semantically wrong.Better: define a third common class, from which both will inherit.
Rule for method overriding:All methods that implement an operation must
have the same protocol.