chapter 3 class diagrams. 2 outline class basics class basics classes classes association...
TRANSCRIPT
Chapter 3Chapter 3
Class DiagramsClass Diagrams
2
OutlineOutline
Class BasicsClass Basics ClassesClasses AssociationAssociation MultiplicityMultiplicity InheritanceInheritance
An Example Class Diagram: An ATM An Example Class Diagram: An ATM SystemSystem
More Details About Class DiagramsMore Details About Class Diagrams
3
Why Do We Need Why Do We Need Class DiagramsClass Diagrams
Class diagrams allow us to denote the Class diagrams allow us to denote the static contents of, and relationships static contents of, and relationships between classes.between classes.
In class diagram, we can show the member In class diagram, we can show the member variables, and member functions of a class.variables, and member functions of a class.
We can show whether one class inherits We can show whether one class inherits from another, or whether it holds a from another, or whether it holds a reference to another.reference to another.
We can depict all the We can depict all the source code source code dependenciesdependencies between classes. between classes.
4
The Basics: Classes (1/3)The Basics: Classes (1/3)
The simplest form of a class The simplest form of a class diagram:diagram:
The class name Dialler is mandatory.The class name Dialler is mandatory.
5
The Basics: Classes (2/3)The Basics: Classes (2/3)
A class icon with three A class icon with three compartments:compartments: 1st: The name of the class1st: The name of the class 2nd: The variables of the class2nd: The variables of the class 3rd: The methods of the class 3rd: The methods of the class
6
The Basics: Classes (3/3)The Basics: Classes (3/3)
Access levels of methods and variables:Access levels of methods and variables: -: private-: private #: protected#: protected +: public+: public
The type of a variable, or a function The type of a variable, or a function argument is shown after the colon argument is shown after the colon following the variable or argument name.following the variable or argument name.
Again, don’t put all variables and Again, don’t put all variables and functions in a class diagram.functions in a class diagram.
7
The Basics: AssociationThe Basics: Association
Associations between classes most Associations between classes most often represent instance variables often represent instance variables that hold references to other object.that hold references to other object.
The number near the arrowhead will The number near the arrowhead will be addressed in the next slide.be addressed in the next slide.
8
The Basics: MultiplicityThe Basics: Multiplicity
The number near the arrowhead The number near the arrowhead represents how many references are represents how many references are held.held.
The star above the arrowhead means The star above the arrowhead means many (i.e., from zero to infinity. In many (i.e., from zero to infinity. In Java, this is most commonly Java, this is most commonly implemented with a implemented with a VectorVector, a , a ListList, or , or some other container type.some other container type.
9
QuestionQuestion
What is a container?What is a container?
10
The Basics: Inheritance The Basics: Inheritance (1/3)(1/3)
Be careful with your arrowheads in Be careful with your arrowheads in UML.UML.
If you draw your arrowheads If you draw your arrowheads carelessly, it may be hard to tell carelessly, it may be hard to tell whether you mean inheritance or whether you mean inheritance or association.association.
11
The Basics: Inheritance The Basics: Inheritance (2/3)(2/3)
All arrowheads point in the direction All arrowheads point in the direction of of source code dependencysource code dependency..
Inheritance arrows point at the base Inheritance arrows point at the base class.class.
12
The Basics: Inheritance The Basics: Inheritance (3/3)(3/3)
Two ways to depict the inheritance Two ways to depict the inheritance between a Java class and a Java between a Java class and a Java interface:interface: Type A:Type A:
Type B (often seen in COM designs): Type B (often seen in COM designs):
13
QuestionQuestion
What is COM?What is COM?
14
An Example Class An Example Class Diagram (1/3)Diagram (1/3)
An ATM system:An ATM system:
15
An Example Class An Example Class Diagram (2/3)Diagram (2/3)
An ATM system (cont.):An ATM system (cont.): WithdrawTransactionWithdrawTransaction talks to a talks to a CashDispenserCashDispenser
interface, but the class which implements interface, but the class which implements the the CashDispenserCashDispenser is not shown in this is not shown in this diagram.diagram.
WithdrawlUIWithdrawlUI will need more than just the will need more than just the two methods shown in Figure 3-8.two methods shown in Figure 3-8.
The convention used in this book:The convention used in this book: Horizontal line: associationHorizontal line: association Vertical line: inheritanceVertical line: inheritance
16
An Example Class An Example Class Diagram (3/3)Diagram (3/3)
The code of the class The code of the class UIUI::
17
The Details: Class The Details: Class Stereotypes Stereotypes
(1/3)(1/3) Appear between guilmette Appear between guilmette
characters, usually above the name characters, usually above the name of the class.of the class.
Two standard stereotypes:Two standard stereotypes: «interface»«interface» «utility»«utility»
18
The Details: Class The Details: Class Stereotypes (2/3)Stereotypes (2/3)
«interface»«interface» All the methods of an «interface» All the methods of an «interface»
classes are abstract.classes are abstract. None of the methods can be None of the methods can be
implemented.implemented. «interface» classes have no instance «interface» classes have no instance
variables other than static variables.variables other than static variables.
19
The Details: Class The Details: Class Stereotypes (3/3)Stereotypes (3/3)
«utility»:«utility»: All the methods of classes are static.All the methods of classes are static.
You can make your own stereotypes, You can make your own stereotypes, but make sure other people know but make sure other people know what your stereotypes mean.what your stereotypes mean.
20
The Details: Abstract The Details: Abstract Classes (1/2)Classes (1/2)
Two ways to denote that a class, or a Two ways to denote that a class, or a method, is abstract:method, is abstract: Write the name in italics.Write the name in italics. Use the {abstract} property.Use the {abstract} property.
21
The Details: Abstract The Details: Abstract Classes (2/2)Classes (2/2)
Difficult to write italics at a whiteboardDifficult to write italics at a whiteboard The {abstract} property is wordy.The {abstract} property is wordy. This book uses the following convention: This book uses the following convention:
22
The Details: Properties The Details: Properties (1/2)(1/2)
can be added to any classcan be added to any class represent extra information that’s represent extra information that’s
not usually part of a classnot usually part of a class You can create your own properties You can create your own properties
at any time.at any time. The {abstract} property is the only The {abstract} property is the only
defined property of UML that java defined property of UML that java programmers would find useful.programmers would find useful.
23
The Details: Properties The Details: Properties (2/2)(2/2)
An example class with four properties:An example class with four properties:
If a property does not have a value, it is If a property does not have a value, it is assumed to take the boolean value true.assumed to take the boolean value true. {abstract} and {abstract = true} are {abstract} and {abstract = true} are
synonyms.synonyms.
24
The Details: Aggregation The Details: Aggregation (1/2)(1/2)
Aggregation is a special from of Aggregation is a special from of association that connotes a association that connotes a “whole/part” relationship.“whole/part” relationship.
The implementation shown in the The implementation shown in the above figure is indistinguishable above figure is indistinguishable from association. That’s a hint.from association. That’s a hint.
25
The Details: Aggregation The Details: Aggregation (2/2)(2/2)
UML does not provide a strong UML does not provide a strong definition for this relationship.definition for this relationship.
The one hard rule regarding The one hard rule regarding aggregation:aggregation: A whole cannot be its own part.A whole cannot be its own part.
26
The Details: Composition The Details: Composition (1/4)(1/4)
Composition is a special form of Composition is a special form of aggregation.aggregation.
The implementation is The implementation is indistinguishable from association.indistinguishable from association.
There can be no cycles of instances.There can be no cycles of instances.
27
The Details: Composition The Details: Composition (2/4)(2/4)
More definition:More definition: An instance of a ward cannot be An instance of a ward cannot be
simultaneously owned by two owners. simultaneously owned by two owners. The following object diagram is illegal. The following object diagram is illegal. However, the corresponding class However, the corresponding class diagram is not illegal.diagram is not illegal.
28
The Details: Composition The Details: Composition (3/4)(3/4)
More definition (cont.):More definition (cont.): The owner is responsible for the lifetime The owner is responsible for the lifetime
of the ward. If the owner is destroyed, of the ward. If the owner is destroyed, the ward must be destroyed with it. If the ward must be destroyed with it. If the owner is copied, the ward must be the owner is copied, the ward must be copied with it.copied with it.
29
The Details: Composition The Details: Composition (4/4)(4/4)
How composition is used to denote How composition is used to denote deep copy:deep copy:
30
The Details: Multiplicity The Details: Multiplicity (1/2)(1/2)
Objects can hold arrays or vectors of Objects can hold arrays or vectors of other objects; or they can hold many other objects; or they can hold many of the same kind of object in of the same kind of object in separate instance variables.separate instance variables. Can be shown by placing a multiplicity Can be shown by placing a multiplicity
expression on the far end of the expression on the far end of the association.association.
31
The Details: Multiplicity The Details: Multiplicity (2/2)(2/2)
Allowable forms of a multiplicity Allowable forms of a multiplicity expression:expression: DigitDigit: the exact number of elements: the exact number of elements * or 0..** or 0..*: zero to many: zero to many 0..10..1: zero or one: zero or one 1..*1..*: one to many: one to many 3..53..5: three to five: three to five 0, 2..5, 9..*0, 2..5, 9..*: silly, but legal: silly, but legal
32
The Details: Association The Details: Association Stereotypes (1/3)Stereotypes (1/3)
Association can be labeled with Association can be labeled with stereotypes that change their stereotypes that change their meaning.meaning.
33
The Details: Association The Details: Association Stereotypes (2/3)Stereotypes (2/3)
«create»«create»: indicates that the target of : indicates that the target of the association is created by the the association is created by the source.source.
«local»«local»: is used when the source class : is used when the source class creates an instance of the target and creates an instance of the target and holds it in a local variable. holds it in a local variable.
«parameter»«parameter»: shows that the source : shows that the source class gains access to the target class gains access to the target instance through the parameter of one instance through the parameter of one of its member functions. of its member functions.
34
The Details: Association The Details: Association Stereotypes (3/3)Stereotypes (3/3)
«delegates»«delegates»: is used when the : is used when the source class forwards a member source class forwards a member function invocation to the target. function invocation to the target. This is This is notnot a standard part of UML. a standard part of UML.
35
The Details: Inner The Details: Inner ClassesClasses
Inner (nested) classes are Inner (nested) classes are represented with an association represented with an association adorned with a crossed circle.adorned with a crossed circle.
36
The Details: Anonymous The Details: Anonymous Inner ClassesInner Classes
UML does not have an official stance UML does not have an official stance on anonymous inner classes.on anonymous inner classes.
The textbook uses the following The textbook uses the following convention:convention:
37
The Details: Association The Details: Association Classes (1/2)Classes (1/2)
Associations with multiplicity tell us Associations with multiplicity tell us that the source is connected to many that the source is connected to many instances of the target, but does not instances of the target, but does not tell us what kind of container class is tell us what kind of container class is used.used.
38
The Details: Association The Details: Association Classes (2/2)Classes (2/2)
Association classes can also be used Association classes can also be used to indicate special forms of to indicate special forms of references, such as weak, soft, or references, such as weak, soft, or phantom references.phantom references.
39
The Details: Association The Details: Association Qualifiers Qualifiers
Association qualifiers are used when Association qualifiers are used when the association is implemented the association is implemented through some kind of key of token, through some kind of key of token, instead of with a normal Java instead of with a normal Java reference.reference.