Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CS 494
Object-Oriented Analysis & Design • How class models are used? Perspectives
• Classes: attributes and operations
• Associations
– Multiplicity
UML Class Models • Generalization and Inheritance
• Aggregation and composition
© 2001 T. Horton
2/7/00 G-1 2/7/00 G-2
A-1
Associations More on Associations
• For “real-world objects” is there an association • Associations should model the reality of the domain
between classes? and allow implementation
• Classes A and B are associated if: • Associations are between classes
– An object of class A sends a message to an object of B – A link connects two specific objects
– An object of class A creates an instance of class B – Links are instances of associations
– An object of class A has an attribute of type B or – Note we could draw an object diagram to show objects
collections of objects of type B and links
– An object of class A receives a message with an • But often interaction diagrams are more useful for
argument that is an instance of B (maybe…)
modeling objects
• Will it “use” that argument?
• Note: In practice, early in modeling, we may not name
• Does an object of class A need to know about some
object of class B? associations
• Note: One may choose to have a dynamic view
associations: if at run-time two objects exchange
messages, their classes must be associated
2/7/00 G-7 2/7/00 G-8
– A range: two dots between a pair of numbers • One book has 1 or more Is A Copy Of
– An arbitrary number: indicated by * symbol copies 1..*
• You may model “inheritance” early but not implement it • Again, just a specific kind of association
– Generalization represents a relationship at the conceptual level
between classes
– Inheritance is an implementation technique
• Generalization is just an association between classes – An object of class A is part of an object of
– But so common we put a “triangle” at the superclass class B
• Note this is a relationship between classes – A part-whole relationship
– So no multiplicities are marked. Why not?
• Put a diamond on the end of the line next to
• Inheritance may not be appropriate when it’s time to
implement the “whole”
– Objects should never change from one subclass to another – Aggregation (hollow diamond): really no
– Composition can be used instead semantics about what this means!
– Composition (solid diamond): a stronger
relationship
2/7/00 G-11 2/7/00 G-12
A-2
Aggregation and Composition (cont’d) Example 1: University Courses
• Composition
– The whole strongly owns the parts • Some instructors are professors, while others
have job title adjunct
– Parts are copied (deleted, etc.) if the whole is
copied (deleted, etc.) • Departments offer many courses, but a
– A part cannot be part of more than one whole course may be offered by >1 department
– Mnemonic: the stronger relationship is • Courses are taught by instructors, who may
indicated by the stronger symbol (it’s solid) teach up to three courses
• Aggregation and composition associations • Instructors are assigned to one (or more)
are not named departments
• They do have multiplicities • One instructor also serves a department chair
• They can be used too often. If in doubt, use a
“plain”, named association. 2/7/00 G-13 2/7/00 G-14
1
1 0..*
Managed By History Log
0..n
History Entry
Code Bug Report
-when : Date
-whatDone : string
A-3
Objects, Object Diagrams Class Attributes, Operations
• Objects drawn • Recall in Java and C++ you may have class
like classes, but
names for all
attributes and class operations
instances – keyword static used
underlined – One attribute for all members of class
• Objects may be – An operation not encapsulated in each object,
“anonymous” but “defined in” that class’ scope
• Attributes are
given values • In UML class diagrams, list these in the class
box’s compartments, but underline them
• Some call this “direction of visibility” • One reason for having an association
• Does each class really store a reference to between classes:
each other? Messages between objects of those classes
• Do we need to decide this now? (When is • But, often “knowledge” indicated by
“now”?) association is only in one direction
• We can add arrows to associations to indicate – Example: In a computer system, a User needs
this access to his/her Password
– What does a line with no arrows mean? – From a Password object we should not be able
to get back to a User!
• Note: Often ignored until design!
2/7/00 G-21 2/7/00 G-22
understand if we simply but a role name at • Booch says: use dependencies not associations
when one class uses another class as an argument in
one or both ends of the line
an operation.
• Often used for other things in UML: A general
relationship between “things” in UML
Student Professor
advises – Often use a stereotype to give more info
• Uses: binding C++ class to template; Java interfaces;
advisee
Student Professor a class only instantiates objects (a factory)
advisor
2/7/00 G-23 2/7/00 G-24
A-4
Stereotypes Stereotypes (cont’d)
A-5
Classes Realize an Interface Tagged Values, Properties
• Every modeling element in UML has its set of
• “Realizes” AKA implements, supports, matches, etc. properties
• This means that class provides all the operations in – Classes have: name, attributes, operations, etc.
the interface (and more?) – What if we want to add our own? (e.g. author?)
– Remember, no implementation in interface definition
• Just add text in curly-brackets, with
• Realization shown with dashed line, hollow arrow name=value, and put below the element name
– Like dependency plus generalization
• Note: These tell you something about the
• Why have this?
model, not about the final system to be built!
– Just factor out common functionality?
– Often used for code generation, version
• Better “pluggability”, extensibility control, etc.
• Example: {abstract} classes instead of italics
• Implementation not provided for one or more • Conditions that restrict Portfolio Corporation
abstract operations is effectively an interface UML User Guide, p. 82 husband 0..1 0..1 wife
– But Java provides a direct implementation {self.wife.gender = female and
self.husband.gender = male}
Student
2/7/00 G-35 2/7/00 G-36
A-6
Example: Ticket Sales
Unused slides follow
s at City
countries. 1 *
Pla
Country Zone 2
• Each team plays a given number of games in
ysI
4
n
1 1
a specific city. Referees are assigned to Represents
Assignment
5
A-7
Qualified Associations Qualified Association Examples:
• Equivalent to programming language idea of lookup, 0..1
map, dictionary, associative array, etc. Show
sales
Ticket
1
• An object is associated with some number of other
objects in a class
– How do we identify which one we want given that
perf: Date, 0..1
association? Show Ticket
seat: Number 1 sales
• The qualifier documents attribute(s) used to identify
which object
– The “key” for “lookup”
• Formally, these are attributes of the association
0..1
RepairDesk jobID: int ReturnedItem
• From textual descriptions or requirements or • Take some concise statement of the requirements
use cases, how do we get classes? • Underline nouns or noun phrases that represent
• Various techniques, and practice! things
– Key Domain Abstractions: – These are candidate classes
A-8