Class diagrams

A class diagram is a structural diagram that is mainly used to provide the design of a piece of object-oriented software. This diagram demonstrates the structure of a software, the attributes and methods of a class, and the relationship between classes in the system. It can be used for development as well as for documentation; software developers frequently use this diagram to get a quick idea of the code and to help fellow developers understand the system. It is also occasionally used by employees involved in the business side of a company.

The following are the three main parts of a class diagram:

  • The class name
  • The attribute section
  • The method section

A class diagram consists of different classes that are represented as boxes or rectangles. A rectangle is normally divided into the aforementioned sections. The first part holds the name of the class, the second portion holds the attributes, and the third section contains the methods.

Let's take a look at an example of a class diagram:

Here, we can see that we have a class called Car, as indicated by the top box. Below that, we have the attributes of that class. We can see that color is the name of an attribute, which has a + sign in front of it, indicating that it is a public variable. We can also see that there is a : (colon) next to the variable name, which is a separator. Whatever is given after the colon represents the type of the variable. In this case, we can see that the color variable is of the string type. The next attribute is company, which is also a variable of the string type. This has a - sign in front of it, which means that it is a private variable. The third attribute is fuel, and we can see that this is a private variable of the integer type.

If we look below the attributes, we will see the methods of the Car class. We can see that it has three methods: move(direction: string)IsFuelEmpty(), and RefilFuel(litre: int). Like the attributes, we can see that the methods have a : (colon). In this case, the type that is given after the colon is the return type of the method. The first method, move, doesn't return anything, so the type is void. In the IsFuelEmpty() method, the return type is Boolean, and this is also the case for the third method. Another thing to note here is the parameters of the methods, which are placed in parentheses after the method name. For example, the move method has a parameter called direction, which is a string type. The RefilFuel(litre: int) method has an int type parameter, which is litre

In the preceding example, we saw how a class is represented in a class diagram. Normally, a system has multiple classes that are related to each other in some way. A class diagram demonstrates the relationships of the classes as well, which gives the viewer a full picture of the system's object relationships. In Chapter 4, Object Collaboration, we learned about the different relationships between classes and objects in object-oriented software. Let's now take a look at how we can represent these different object relationships using class diagrams.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.57.16