Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Class Diagrams. 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance.

Similar presentations


Presentation on theme: "Chapter 3 Class Diagrams. 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance."— Presentation transcript:

1 Chapter 3 Class Diagrams

2 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance An Example Class Diagram: An ATM System An Example Class Diagram: An ATM System More Details About Class Diagrams More Details About Class Diagrams

3 3 Why Do We Need Class Diagrams Why Do We Need Class Diagrams Class diagrams allow us to denote the static contents of, and relationships between classes. Class diagrams allow us to denote the static contents of, and relationships between classes. In class diagram, we can show the member variables, and member functions of a class. In class diagram, we can show the member variables, and member functions of a class. We can show whether one class inherits from another, or whether it holds a reference to another. We can show whether one class inherits from another, or whether it holds a reference to another. We can depict all the source code dependencies between classes. We can depict all the source code dependencies between classes.

4 4 The Basics: Classes (1/3) The Basics: Classes (1/3) The simplest form of a class diagram: The simplest form of a class diagram: The class name Dialler is mandatory. The class name Dialler is mandatory.

5 5 The Basics: Classes (2/3) A class icon with three compartments: A class icon with three compartments: 1st: The name of the class 1st: The name of the class 2nd: The variables of the class 2nd: The variables of the class 3rd: The methods of the class 3rd: The methods of the class

6 6 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 argument is shown after the colon following the variable or argument name. The type of a variable, or a function argument is shown after the colon following the variable or argument name. Again, don’t put all variables and functions in a class diagram. Again, don’t put all variables and functions in a class diagram.

7 7 The Basics: Association Associations between classes most often represent instance variables that hold references to other object. Associations between classes most often represent instance variables that hold references to other object. The number near the arrowhead will be addressed in the next slide. The number near the arrowhead will be addressed in the next slide.

8 8 The Basics: Multiplicity The number near the arrowhead represents how many references are held. The number near the arrowhead represents how many references are held. The star above the arrowhead means many (i.e., from zero to infinity. In Java, this is most commonly implemented with a Vector, a List, or some other container type. The star above the arrowhead means many (i.e., from zero to infinity. In Java, this is most commonly implemented with a Vector, a List, or some other container type.

9 9 Question What is a container? What is a container?

10 10 The Basics: Inheritance (1/3) Be careful with your arrowheads in UML. Be careful with your arrowheads in UML. If you draw your arrowheads carelessly, it may be hard to tell whether you mean inheritance or association. If you draw your arrowheads carelessly, it may be hard to tell whether you mean inheritance or association.

11 11 The Basics: Inheritance (2/3) All arrowheads point in the direction of source code dependency. All arrowheads point in the direction of source code dependency. Inheritance arrows point at the base class. Inheritance arrows point at the base class.

12 12 The Basics: Inheritance (3/3) Two ways to depict the inheritance between a Java class and a Java interface: Two ways to depict the inheritance between a Java class and a Java interface: Type A: Type A: Type B (often seen in COM designs): Type B (often seen in COM designs):

13 13 Question What is COM? What is COM?

14 14 An Example Class Diagram (1/3) An ATM system: An ATM system:

15 15 An Example Class Diagram (2/3) An ATM system (cont.): An ATM system (cont.): WithdrawTransaction talks to a CashDispenser interface, but the class which implements the CashDispenser is not shown in this diagram. WithdrawTransaction talks to a CashDispenser interface, but the class which implements the CashDispenser is not shown in this diagram. WithdrawlUI will need more than just the two methods shown in Figure 3-8. WithdrawlUI will need more than just the two methods shown in Figure 3-8. The convention used in this book: The convention used in this book: Horizontal line: association Horizontal line: association Vertical line: inheritance Vertical line: inheritance

16 16 An Example Class Diagram (3/3) The code of the class UI : The code of the class UI :

17 17 The Details: Class Stereotypes (1/3) Appear between guilmette characters, usually above the name of the class. Appear between guilmette characters, usually above the name of the class. Two standard stereotypes: Two standard stereotypes: «interface» «interface» «utility» «utility»

18 18 The Details: Class Stereotypes (2/3) «interface» «interface» All the methods of an «interface» classes are abstract. All the methods of an «interface» classes are abstract. None of the methods can be implemented. None of the methods can be implemented. «interface» classes have no instance variables other than static variables. «interface» classes have no instance variables other than static variables.

19 19 The Details: Class 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, but make sure other people know what your stereotypes mean. You can make your own stereotypes, but make sure other people know what your stereotypes mean.

20 20 The Details: Abstract Classes (1/2) Two ways to denote that a class, or a method, is abstract: Two ways to denote that a class, or a method, is abstract: Write the name in italics. Write the name in italics. Use the {abstract} property. Use the {abstract} property.

21 21 The Details: Abstract Classes (2/2) Difficult to write italics at a whiteboard Difficult 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 22 The Details: Properties (1/2) can be added to any class can be added to any class represent extra information that’s not usually part of a class represent extra information that’s not usually part of a class You can create your own properties at any time. You can create your own properties at any time. The {abstract} property is the only defined property of UML that java programmers would find useful. The {abstract} property is the only defined property of UML that java programmers would find useful.

23 23 The Details: Properties (2/2) An example class with four properties: An example class with four properties: If a property does not have a value, it is assumed to take the boolean value true. If a property does not have a value, it is assumed to take the boolean value true. {abstract} and {abstract = true} are synonyms. {abstract} and {abstract = true} are synonyms.

24 24 The Details: Aggregation (1/2) Aggregation is a special from of association that connotes a “whole/part” relationship. Aggregation is a special from of association that connotes a “whole/part” relationship. The implementation shown in the above figure is indistinguishable from association. That’s a hint. The implementation shown in the above figure is indistinguishable from association. That’s a hint.

25 25 The Details: Aggregation (2/2) UML does not provide a strong definition for this relationship. UML does not provide a strong definition for this relationship. The one hard rule regarding aggregation: The one hard rule regarding aggregation: A whole cannot be its own part. A whole cannot be its own part.

26 26 The Details: Composition (1/4) Composition is a special form of aggregation. Composition is a special form of aggregation. The implementation is indistinguishable from association. The implementation is indistinguishable from association. There can be no cycles of instances. There can be no cycles of instances.

27 27 The Details: Composition (2/4) More definition: More definition: An instance of a ward cannot be simultaneously owned by two owners. The following object diagram is illegal. However, the corresponding class diagram is not illegal. An instance of a ward cannot be simultaneously owned by two owners. The following object diagram is illegal. However, the corresponding class diagram is not illegal.

28 28 The Details: Composition (3/4) More definition (cont.): More definition (cont.): The owner is responsible for the lifetime of the ward. If the owner is destroyed, the ward must be destroyed with it. If the owner is copied, the ward must be copied with it. The owner is responsible for the lifetime of the ward. If the owner is destroyed, the ward must be destroyed with it. If the owner is copied, the ward must be copied with it.

29 29 The Details: Composition (4/4) How composition is used to denote deep copy: How composition is used to denote deep copy:

30 30 The Details: Multiplicity (1/2) Objects can hold arrays or vectors of other objects; or they can hold many of the same kind of object in separate instance variables. Objects can hold arrays or vectors of other objects; or they can hold many of the same kind of object in separate instance variables. Can be shown by placing a multiplicity expression on the far end of the association. Can be shown by placing a multiplicity expression on the far end of the association.

31 31 The Details: Multiplicity (2/2) Allowable forms of a multiplicity expression: Allowable forms of a multiplicity expression: Digit: the exact number of elements Digit: the exact number of elements * or 0..*: zero to many * or 0..*: zero to many 0..1: zero or one 0..1: zero or one 1..*: one to many 1..*: one to many 3..5: three to five 3..5: three to five 0, 2..5, 9..*: silly, but legal 0, 2..5, 9..*: silly, but legal

32 32 The Details: Association Stereotypes (1/3) Association can be labeled with stereotypes that change their meaning. Association can be labeled with stereotypes that change their meaning.

33 33 The Details: Association Stereotypes (2/3) «create»: indicates that the target of the association is created by the source. «create»: indicates that the target of the association is created by the source. «local»: is used when the source class creates an instance of the target and holds it in a local variable. «local»: is used when the source class creates an instance of the target and holds it in a local variable. «parameter»: shows that the source class gains access to the target instance through the parameter of one of its member functions. «parameter»: shows that the source class gains access to the target instance through the parameter of one of its member functions.

34 34 The Details: Association Stereotypes (3/3) «delegates»: is used when the source class forwards a member function invocation to the target. This is not a standard part of UML. «delegates»: is used when the source class forwards a member function invocation to the target. This is not a standard part of UML.

35 35 The Details: Inner Classes Inner (nested) classes are represented with an association adorned with a crossed circle. Inner (nested) classes are represented with an association adorned with a crossed circle.

36 36 The Details: Anonymous Inner Classes UML does not have an official stance on anonymous inner classes. UML does not have an official stance on anonymous inner classes. The textbook uses the following convention: The textbook uses the following convention:

37 37 The Details: Association Classes (1/2) Associations with multiplicity tell us that the source is connected to many instances of the target, but does not tell us what kind of container class is used. Associations with multiplicity tell us that the source is connected to many instances of the target, but does not tell us what kind of container class is used.

38 38 The Details: Association Classes (2/2) Association classes can also be used to indicate special forms of references, such as weak, soft, or phantom references. Association classes can also be used to indicate special forms of references, such as weak, soft, or phantom references.

39 39 The Details: Association Qualifiers Association qualifiers are used when the association is implemented through some kind of key of token, instead of with a normal Java reference. Association qualifiers are used when the association is implemented through some kind of key of token, instead of with a normal Java reference.


Download ppt "Chapter 3 Class Diagrams. 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance."

Similar presentations


Ads by Google