Attributes and operations are listed one per line in their respective compartment. Both attributes and operations must have a type and a level of visibility.
Types are either the data type of an attribute, or the return type of an operation. The type is written after the attribute or operation’s name with a colon in between, as you saw in the previous section.
Data types and return types include:
If your operation does not return any value, then its type is “void”. Except constructors, they do not have return types at all.
Each attribute and operation also has a visibility. The visibility in classes are indicated as follows:
There is no default visibility in class diagrams. If you do not indicate a visibility, it is assumed that you haven’t decided on it yet.
The definition for attributes described above are mostly enough. However, more detail can be added by means of initial values, multiplicities and derived attributes.
An initial value for an attribute is the value that the variable will start off with when the object is instantiated. It is indicated by an equals followed by the value after the attribute data type. This is shown below.
As an example, a customer might get R250 free credits when they register for the first time, and their title will be set to “Mx”. As such a credit variable in some Customer class can be set to R250, and title will be set to “Mx.” when an object of said class is instantiated. This is shown below.
Take note that the string value is surrounded by double quotes, while the number value is not.
You might have arrays in your attributes. For an array, you need to show how many elements will be stored. This is shown using square brackets after the attribute name. Inside the brackets you will show the minimum and the maximum number of elements in this array in the form shown below.
The minimum value can be any value from 0. The maximum value should be larger than the minimum value, up to any value or * for unlimited.
For example, our customer might be able to store unlimited wish list products, but has to have at least 1 credit card, up to a maximum of 3.
Note the multiplicity of [0..*] for our “wishlist” indicates that the wishlist does not have to have any products, but can unlimited products stored. The “creditcards” can store up to a maximum of 3 credit cards, but requires at least one.
Some attributes can be calculated from other attributes. You indicated this with a forward slash in front of the attribute name, along with a note indicating how the derived attribute is calculated. For example, your age can be determined from your date of birth, as shown here.
Did you notice anything missing in from the operations in the examples above? You have? I knew you had it in you.
Unlike attributes, your operations will need more information namely the parameters that need to be passed to the operation (method).
Each operation can include a list of parameters. These parameters show the name of the parameter and its data type in the form.
For example, when adding a credit card to a customer, we need to pass the credit card number, expiry date, and credit card name.
Now I can pass those three parameters to the “addCC” method. Note how the parameter list is split over two lines. You can do this to prevent your classes from becoming extremely wide.
In your methods, you can group your operations into arbitrary categories. This is purely aesthetic (for making pretty) and has no impact on the actual implementation of the classes. Each category is surrounded by double angle brackets (“«category»”), and then looks like the example below.
You can use these groupings for categories such as constructors, query methods, update methods, helper functions, etc. You do not have to show all get- and set-methods in your class diagram, unless they perform a function different to how you would expect.
Let’s go through our Customer example again.
You can see the different groupings for customer. There are no normal get- and set methods. Instead, the query operation, “getFormattedTelephoneNo”, will return the telephone number as a string formatted with the international dialling code. The update method, “addCC”, does a validation to see if the ccNumber is correct before adding the credit card. Therefore, it is not a normal set method. sendSMS is pretty self-explanatory.
You see the methods have now been grouped. Easy enough, I would say.