DEV Community

nadirbasalamah
nadirbasalamah

Posted on • Updated on

Introduction to Class Diagram

There are many steps when developing a software. One of those steps is software design. In software design step, there are many diagrams that used and one of those is class diagram.

Class Diagram

Class diagram is a diagram that represents relation between many entities that involved in a software. The class diagram consist of attribute and behavior. The attribute represented as a field and behavior represented as a method. This is the structure of class diagram.

Class Diagram Structure

This is the scope notations that can be used in fields or methods inside class diagram.

Symbol Scope Description Example
+ public Field or method is available publicly +setName()
- private Field or method only available for its own class -id: String
# protected Field or method is available for its own class and its sub classes #walk()
~ package Field or method is available in a same package ~getData(): Data

This is the example of class diagram that represents book entity.

Book Entity

Based on the class diagram above, there are three attributes or fields in a book including id, title and author. These three fields has a private scope. In Book class there is a one method called getData() that has a return type is a book data (Book).

Relation in Class Diagram

There are many relationship types in class diagram including generalization, association, aggregation and composition.

Generalization

This relationship is used to define an inheritance mechanism in class diagram. In this example, the generalization relationship is defined between Vehicle class and Car class.

Generalization Example

Association

This relationship is used to define a single relationship between classes that involved each other. In this example, the association relationship is used by User class and Blog class.

Association Example

Aggregation

This relationship is used to define a relationship for a class that is a part of the another class. In this example, the aggregation relationship is used by Car class and Machine class.

Aggregation Example

Composition

This relationship is used to define a relationship between classes that involved each other and cannot be separated. In this example, the composition relationship is used by Course class and Teacher class.

Composition Example

Create a Class Diagram

There are many tools that can be used to create a class diagram like StarUML, Draw.io and other software tools.

These are the steps to create a class diagram:

  1. Define some entities that involved in a software or application. The entity could become concrete entitiy like book, user or abstract entity like course that taken by student.

  2. Define the attributes and behaviors of an entity. Attributes described as a fields in class whilst behaviors described as a methods in class.

  3. Define the scope for the fields and methods. In general, the scope for the fields is private and scoe for the methods is public or protected.

  4. Define the relationships between classes.

  5. Review the class diagram that already created to make sure the diagram is created correctly. The diagram must be correct in terms of syntax and semantics.

Correct in terms of syntax means that a diagram is defined with the correct notation or symbol.

Correct in terms of semantic means that a diagram is understandable.

Notes

  • Class diagram can be used to define the relationship between entities that involved in a software.

  • There are many relationships that can be used in class diagram.

  • Generalization can be used to define inheritance relationship between classes.

  • Association can be used to define single relationship between classes.

  • Aggregation can be used to define relationship for a class that is a part of the another class.

  • Composition can be used to define relationship between classes that involved each other and cannot be separated.

Sources

I hope this article is helpful to understand the concept of class diagram. If you have any thoughts or comments you can write in the discussion section below.

Discussion (0)