Object Modeling (2)

advertisement
Object Modeling (2)
Chapter 3 (2)
Part 1: Modeling Concepts
Object-Oriented Modeling and Design
Byung-Hyun Ha (bhha@pusan.ac.kr)
Lecture Outline
 Introduction
 Links and associations
 Advanced link and association concepts
 Generalization and inheritance
 Remaining parts
Introduction
 Class and objects
 A class is abstraction or specification of a group of similar objects
 e.g. Person vs. Joe, Mary, …
Person
name: string
age: integer
(Person)
Joe Smith
24
(Person)
Mary Sharp
52
 e.g. Every instance of our Vector has x, y, and length().
public class Vector {
double x;
double y;
double length() {
double len = Math.sqrt(x * x + y * y);
return len;
}
}
Introduction
 Association vs. links
 An association is abstraction or specification of a group of similar
links
 e.g. Has-capital
Country
Has-capital
name
City
name
(Country)
Canada
Has-capital
(City)
Ottawa
(Country)
France
Has-capital
(City)
Paris
(Country)
Senegal
Has-capital
(City)
Dakar
Introduction
 Association vs. links (cont’)
 e.g. Intersects
Intersects
Line
name
2+
Point
name
Links and Associations (cont’)
 Multiplicity specification
 How many instances of one class may related to a single
instance of an associated class
 One or many
loves
Playboy
Girl
 Optional
loves
Romanticist
Girl
 Numbers
Boy
3-5
loves
1+
Girl
Links and Associations
 Multiplicity depends on problem
 e.g. tax collection application
Person
Works-for
Company
 e.g. auto workers’ union
Person
Works-for
Company
 It exposes hidden assumption built into the model
Advanced Link and Association Concepts
 Role
 One end of association
 Role name
 A name that uniquely identifies one end of an association
Person
employee
employer
Works-for
Company
owner
User
Directory
authorized user
contents
container
Advanced Link and Association Concepts
 Role name
 All role names on the far end of associations attached to a class
must be unique
 The role name is a derived attribute of the source class
 Role name for n-ary association
 Association of degree 3 or more cannot simply be traversed from
one end to another as binary associations can
Project
Language
Person
Advanced Link and Association Concepts
 Link attributes
 Properties of a link in an association
File
Accessible by
User
access permission
/etc/termcap
/etc/termcap
/usr/doe/.login
(read)
(read-write)
(read-write)
John Doe
Mary Brown
John Doe
Advanced Link and Association Concepts
 Link attributes for one-to-many association
 e.g. works-for and manages
Person
boss
Manages
name
social security no.
address
worker
performance rating
Works-for
Company
name
address
salary
job title
Advanced Link and Association Concepts
 Folding link attributes
 It is possible to fold link attributes for one-to-one and one-tomany associations into the class opposite the “one” site
 How is it possible and which one is preferred?
Person
name
social security no.
address
Person
name
social security no.
address
salary
job title
Works-for
Company
name
address
salary
job title
Works-for
Company
name
address
Advanced Link and Association Concepts
 Link attributes for ternary association
Team
Pitcher
Year
wins
losses
Harry Eisenstat
Harry Eisenstat
Willis Hudlin
Willis Hudlin
Willis Hudlin
Willis Hudlin
Cleveland Indians
Detroit Tigers
Cleveland Indians
Cleveland Indians
Washington Senators
St. Louis Browns
1939
1939
1939
1940
1940
1940
W
6
2
9
2
1
0
L
7
2
10
1
2
1
Advanced Link and Association Concepts
 Modeling an associations as a class
 Each link becomes one instance of the class
 It is useful when links can participate in associations with other
objects or when links are subject to operations
User
Authorized on
Authorization
priority
privileges
start session
home directory
Director
Workstation
Advanced Link and Association Concepts
 Ordering
 A special kind of constraint
{ordered}
Window
Visible-on
Screen
Advanced Link and Association Concepts
 Qualification
 A qualified association relates two object classes and a qualifier
 The qualifier distinguishes among the set of objects at the many
end of an association
Directory
Directory
File
file name
File
Advanced Link and Association Concepts
 Examples
 Ticker symbol of company in stock exchange
Stock
exchange
Stock
exchange
ticker symbol
lists
lists
Company
ticker symbol
Company
 Company and office
Company
office
organization
officer
Person
Advanced Link and Association Concepts
 Aggregation
 “Part-whole” or “a-part-of” relationship
 Used for components and assembly
• Existence of a part depends on existence of whole
 Tightly coupled form of association extra semantics
• Transitivity
• If A is part of B and B is part of C, then A is part of C
• Antisymmetric
• If A is part of B, then B is not part of A
• Propagation of properties
• e.g. location of a door handle is obtained from the door; door
obtains its properties from the car, …
Document
Paragraph
Sentence
Generalization and Inheritance
 Generalization
 The relationship between a class and one or more refined
version of it
• superclass and subclass
 “is-a” relationship
 Transitive
• ancestor and descendant
• An instance of a subclass is simultaneously an instance of all its
ancestor classes
Equipment
name
manufacturer
weight
cost
Pump
flow rate
Tank
volume
…
Generalization and Inheritance
 Examples
 Fig. 3.23 and 3.24
 Some guidelines
 Do not nest subclasses too deeply
 “An inheritance hierarchy that is two or three levels deep is
certainly acceptable; ten levels deep is probably excessive; five
or six levels may or may not proper”
 Inheritance, generalization, and specialization
 Different viewpoints of the same relationship
 A subclass is a special case of its superclass
 Don’t borrow a class using inheritance, when the new class is not
really a special case of the original class!
Remaining Parts
 Grouping constructs
 Module and sheet
 A sample object model
 Fig. 3.25: object model for window system
 Practical tips
 …
Homework
 HW7
 Exercise 3.6 (p. 50)
 HW8: extend E3.6
a. Modify the class diagram in Figure E3.6 (p. 51) so as to handle
unary minus and functions (e.g., sqrt(x) for calculating square
root of x) with arbitrary number of arguments.
b. Prepare an instance diagram for the class diagram you made for
the expression -(B + sqrt(B*B – 4*A*C))/(4*A*A).
Download