In UML, the class diagram represents the static structure of a system, presenting its classes, attributes, operations and the relationships between classes. Being considered the main diagram of the language.

General concept

According to OMG (Object Management Group) the class diagram:

During the system modeling process the business concepts - data, information; they must be analyzed in order to recognize their characteristics - states or values ​​of their properties; and their behaviors.

First, these concepts are identified in the requirements, that is, in scenarios. For example: where warehouse workers work with parts, replacement requests, and supply orders; the financial area manages invoices and receipts; sales organizes orders and customers; among other processes.

In addition to characteristics and behaviors, business concepts were related to each other. The class diagram is used to make it easier to understand and visualize this structure of concepts and their relationships.

The following video lessons present theory and practice regarding the diagram. They describe its elements: classes, attributes, methods, relationships; and good practices in their use.

After the video lessons, the article continues ;-)

Vídeo

Class Diagram Theory

Part I

Part II

Using the Class Diagram in practice

In the following video lesson is a demonstration of the resolution of a simple case study of Media Library Management:

Following are the elements and how to demonstrate relationships and constraints in the Class Diagram.

Elements of the class diagram

Classes

In object-oriented modeling, a class is a “model”, which describes state and common behavior. It is an abstraction that defines a type of object and its characteristics (attributes) and the actions that the object can perform (methods).

In the class diagram they are represented by a rectangle divided into three sections:

  • Class Name - in the first section. It is also possible to identify the class with stereotypes (watch the video lessons to understand stereotypes)
  • Properties or attributes - in the second section the properties, their visibility and their typing are defined
  • Methods or actions - in the third section the signatures of the methods are described, that means: their visibility, parameters and return typing

Representation of a class in a class diagram
Representation of a class in a class diagram

Visibility

Visibility is defined on the elements of a class in order to determine whether (or not) they can be accessed by other classes, in other words, it defines the level of access to the elements of the class.

The visibilities can be defined as:

  • + {public}: the element can be used by the object to which it belongs and by any client object, that is, any object that wants to access the element of the class/object.
  • # {protected}: the element can only be used by the object in which it belongs by its subclasses if they exist.
  • - {private}: the element can only be used by the object to which it belongs, that is, not even its subclasses can access the element.
  • ~{package}: visible to all classes of the same package (package).

Attributes

An attribute is a property of an object (defined in the class), eg name, age (person attributes). Each attribute has a value for each instance, that is, each creation of an object from a class.

For example: for the joao object created from the Student class above, the value for the name attribute is “John”.

An attribute can have a default value, that is, the definition of a default value that is assigned to it on object instantiation.

Also, attributes can be

  • Primitive types
  • Objects of other classes: that is, relationship with other complex objects
  • Derivatives:
    • Can be used as an indicator to the implementer that the attribute may not be strictly necessary.
    • Symbol indicating a derived attribute: forward slash (/)

In the following image an example of two classes with their attributes. The Car class has two private attributes ( - ), one of which is a primitive type (integer) and the other is an object of type Color. The Cor class, which also has two private attributes, both objects of type String.

Example of classes and their attributes
Example of classes and their attributes

ARTICLE UNDER DEVELOPMENT