Upload
megan
View
32
Download
1
Embed Size (px)
DESCRIPTION
Enterprise & Application Frameworks. Dr. M.E. Fayad, Professor Computer Engineering Department – RM# College of Engineering San José State University One Washington Square San José, CA 95192-0180 URL: http://www.cmpe.sjsu.edu/~fayad. Lesson 5: Object-Oriented Concepts-3. 2. - PowerPoint PPT Presentation
Citation preview
Fall 2002 SJSU -- CmpE
Enterprise & Application Frameworks
Dr. M.E. Fayad, ProfessorComputer Engineering Department – RM#
College of Engineering
San José State University
One Washington Square
San José, CA 95192-0180
URL: http://www.cmpe.sjsu.edu/~fayad
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S2 OO Concepts-3
2
Lesson 5:Object-Oriented Concepts-3
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S3 OO Concepts-3
Lesson Objectives
Objectives
3
Discuss UML Associations Discuss UML – aggregation Understand UML- inheritance Explore use case concepts Understand How to identify UML
Artifacts
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S4 OO Concepts-3
An association shows a two-way relationship between
objects (instances) of two or more classes and requiring
special implementation to ensure integrity.
A particular instance of an association is often called a link.
Associations between classes are required if the objects
need to communicate.
Associations are often named, and have role-names for
each side of the link.
4
Associations
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S5 OO Concepts-3
5
More on Associations
Association Name
Person
nameageaddress
Car
modelyear# of doors
registeredCar owner
Vehicle Registration
Roles Names
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S6 OO Concepts-3
Simple example:
An association has a name and a numerical specification (multiplicity indication) of how many objects on one side of the association are connected with how many objects on the other side.
Associations are called use relationships – even if this may seem somewhat cheeky in the example below
6
More on Associations
Window GeomFiguredisplays 0..*
Company Personemploys 0..*
Employer Employee
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S7 OO Concepts-3
Associations are directed (one way), bidirectional, and undirected.
UML makes no distinction between bidirectional and undirected associations.
7
More on Associations
Class1 Class2
StereotypeRelationName{Constraints} *
role1 Role2: interface
Multiplicity
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S8 OO Concepts-3
Recursive association: A class has a relation with itself.
It is also possible to model associations that are valid only temporarily “stereotype or temporary”
Constraints may be used to restrict the relation under specific aspects.
A role name describes how the object is seen by the opposite object in the association.
An association can be described in more detail by means of constraints, tagged values, and stereotypes.
Stereotypes are noted before or above the relation name, constraints, and tagged values after or below the name. 8
More on Associations
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S9 OO Concepts-3
Directed association is a one-way association, in which one side knows the other, but not vice versa.
Multiplicity Specification:
– 1 exactly one
– 0, 1 zero or one
– 0..4 between zero and four
– 3, 7 either three or seven
9
More on Associations
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S10 OO Concepts-3
More multiplicity specifications:
– 0..* greater than or equal to zero (default)
– * ditto
– 1..* greater than or equal to one
– 0..3, 7, 9..* between zero and three, or exactly seven, or greater than or equal to nine.
10
More on Associations
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S11 OO Concepts-3
Recursive associations are associations in which one class is involved.
11
More on Associations
Employee
namestaffNoroomNo
Person
Patient
1manager
reports to
leads
office clerk
has
* *relative
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S12 OO Concepts-3
An n-ary association is like a common (binary) association, except that more than two association roles involved in it.
12
N-ary Association
Class1 Class2
Class3
N-aryassociation
Ternaryassociation
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S13 OO Concepts-3
13
More on n-ary Association
datetrainNo
TraincarriageNo
seatNo
Seat
nametitle
Passenger
Reservation
1 1..*
1..*
Ternaryassociation
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S14 OO Concepts-3
14
More on Associations• Recursive Associations• N-ary Associations• Attributed Associations (Association Class)• Association Constraints• Qualified Associations• Derived Associations• Directed Associations• Ordered Associations• Realization/Refinements• Dependency
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S15 OO Concepts-3
More on Associations
Associations are usually bidirectional
Allows each object involved in the relationship
to refer to the object to which it is related.
Bidirectional association means at the
relationship has an inverse.
Associations do not have to be bidirectional. 15
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S16 OO Concepts-3
Man WomanMarriage
husband wife
Screen WindowWindows
Nets UserAuthorization
Car Trailer
Association Type
One-to-One
One-to-Many(One-to-Zero-or-More)
Many-to-Many
Zero or One 16
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S17 OO Concepts-3
Square LineSides
father mother
children
Secure Room PersonOccupants
Male FemaleTraditionalFamily
Association Type
Specified the # of instances on the “many” side
Specify the possible # ofinstances using “or”
Ternary relationship
Child 17
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S18 OO Concepts-3
Server Client
Connection
baudRateprotocolwireType
disconnectrerouteLink
• These attributes don’t belongin either the Client or Serverclass.• They are attributes of the connection itself.
• The association class can have behaviors as well as attributes.
Connection
Association Class
18
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S19 OO Concepts-3
Aggregation is a special form of association.
Aggregation is used when the relationship is
“part/whole” or “contains/is-part-of”
Aggregation is transitive and operations on
“whole” often cascade down to “parts”.
19
Aggregation & Composite
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S20 OO Concepts-3
20
Aggregation & Composite
Server
Client
Aggregation
52
“whole”
“part”
Computer
CPU Disk Drive Keyboard Mouse
*
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S21 OO Concepts-3
Special diamond symbol used on “whole” side to indicate
aggregation.
Aggregation is an anti-symmetric that is, if A is part of B, then B is not
part of A.
DO NOT confuse aggregation with generalization
An essential property of aggregates is that the whole acts as a proxy
for its parts.
A composition is a strict form of aggregation, in which the parts are
existence-dependent on the entirely
21
Aggregation & Composite
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S22 OO Concepts-3
22
Aggregation & Composite
Entirely
Existence-DependentPart
Part
Composition
Aggregation
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S23 OO Concepts-3
Mr. Clinton’s hand is part-of Mr. Clinton & Mr.
Clinton is part-of U.S.A.
Therefore:
Mr. Clinton’s hand is part-of U.S.A. 23
Aggregation Problem
WARNING
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S24 OO Concepts-3
24
Keypad
Number Keys Total Key Subtotal Key
Display Drawer
display display displaydrawer
*
Cash Register Example
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S25 OO Concepts-3
Attributes are simply the information associated with the object.
The data type used to hold the attributes is often a fundamental type, such as int or char.
Sometimes the attribute can be a non-fundamental type, such as String type and Address type.
Avoid using attributes which might be better implemented as an association to a new class.
25
Attributes
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S26 OO Concepts-3
x
26
More on Attributes
TV
String modelString serialNumString manName
For example:
• Why? Using an association to a Manufacturer class, the name and address of each manufacturer will be stored in one place rather than in each of the TV objects made by that manufacturer.
TV
String modelString serialNum
Manufacturer
String nameAddress address
product manu- facturer
Using Attributes Using Association
*
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S27 OO Concepts-3
27
More on Attributes
Employee
String namefloat salarylong clearanceNum
If attributes only make sense in some instances of a given class but not in
others. It will make sense to split the single class into two classes or more.
Employee
String namefloat salary
ClearedEmployee
long clearanceNum
openVault()
Split into two classes
• Not all employees have clearances
inheritance
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S28 OO Concepts-3
When a relationship exists between classes such that
lower-level classes (called subclasses) share certain
attributes and behaviors which can be defined once in a
higher-level class (called superclasses).
Subclasses inherit properties (attributes and behaviors)
of its superclass and then adds its own unique properties
and modifies any inherited properties.
This is called Generalization or Inheritance.
28
Inheritance
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S29 OO Concepts-3
29
More on Inheritance
Window
sizeicon
paintWindow
bitmaps
textWindow
contents
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S30 OO Concepts-3
30
Aggregation vs. Inheritance
Aggregation Inheritance
Instances of distinct classes Instances of a single class
“a-part-of” (APO) “a-kind-of” or “is-a” (AKO)
Contains superclass-of
Aggregate + parts Superclass + subclass
“and” relationship “or” relationship
Propagated only if specified Inheritance
Fall 2002 SJSU -- CmpE M.E. Fayad L5-S31 OO Concepts-3
• What are the other terms for attributes & operations?
• Define an object and a class in UML
• What is an association? Give examples
• Associations are usually bidirectional. Please describe.
• Describe the basic difference between aggregation and inheritance. What do they have in common?
• What do you think “multiple inheritance” means?
• Which type of association is more likely to yield savings in the amount of code required for implementation? why?
• Define the process of identifying objects, classes, inheritance, associations & aggregation, attributes, and behaviors
• Define Polymorphism with examples
• What do we mean by saying “Model/View/Controller”? 31
Discussion Questions