UML Class Diagram with a Property
Figure 4.9 contains an updated UML class diagram for the version of class GradeBook in Fig. 4.7. We model properties in the UML as attributesthe property (in this case, CourseName) is listed as a public attributeas indicated by the plus (+) signpreceded by the word "property" in guillemets (« and »). Using descriptive words in guillemets (called stereotypes in the UML) helps distinguish properties from other attributes and operations. The UML indicates the type of the property by placing a colon and a type after the property name. The get and set accessors of the property are implied, so they are not listed in the UML diagram. Class GradeBook also contains one public method DisplayMessage, so the class diagram lists this operation in the third compartment. Recall that the plus (+) sign is the public visibility symbol.
Figure 4.9. UML class diagram indicating that class GradeBook has a public CourseName property of type string and one public method.
(This item is displayed on page 143 in the print version)
In the preceding section, you learned how to declare a property in C# code. You saw that we typically name a property the same as the instance variable it manipulates, but with a capital first letter (e.g., property CourseName manipulates instance variable courseName). A class diagram helps you design a class, so it is not required to show every implementation detail of the class. Since, an instance variable that is manipulated by a property is really an implementation detail of that property, our class diagram does not show the courseName instance variable. A programmer implementing the GradeBook class based on this class diagram would create the instance variable courseName as part of the implementation process (as we did in Fig. 4.7).
In some cases, you may find it necessary to model the private instance variables of a class that are not properties. Like properties, instance variables are attributes of a class and are modeled in the middle compartment of a class diagram. The UML represents instance variables as attributes by listing the attribute name, followed by a colon and the attribute type. To indicate that an attribute is private, a class diagram would list the private visibility symbola minus sign ()before the attribute's name. For example, the instance variable courseName in Fig. 4.7 would be modeled as "- courseName : string" to indicate that it is a private attribute of type string.