How to Transform UML in Java

Rashmi Patidar Feb 15, 2024
How to Transform UML in Java

Unified Modeling Language is an acronym for UML, is a standard language for the representation and documentation of software systems. The models bring better understandability among developers. The diagrams help in visualizing, expressing the needs, and specifying the constructs of the software system.

The diagram is helpful to the business analyst, designers, testers, quality analysts, customers, technical authors.

UML is a high-level term for various types of diagrams under this category. A variety of mapping needs to be taken care of while interpretation of UML diagram. Let’s have a detailed look at the UML diagram and conversion of them.

UML Representation of a college

In the above diagram, there are various entities involved. So the first step for conversion is to create a class from the below entities. These entities are there in yellow. The class contains the attributes given in the diagram. Also, the data type for the particular field is present in front.

  1. For creating a class, go to the editor say Intellij IDE.
  2. Create a new class named University, College, Students, and Department.
  3. Enter the fields with the datatype in the format as private String university Id type.
  4. Select the newly created fields and right-click over the screen or press the Alt+Insert key.
  5. A generate pop-up will flash on the screen.
  6. Scroll down to Getter and Setter
  7. Click Ok at the bottom.
  8. Similarly, proceed with the other classes also.

The process generates all the classes and the fields along with getters and setters methods. Now check over the relation between the tables.

As to the one end of the class | pipe symbol is visible. To the other end of the line, a symbol is visible over a line making the sign look-alike a three-foot system, often called a crowfoot symbol. The relation depicts a one-to-many type of relationship. The one-to-many relationship denotes there can be multiple entities present for a single entity. In our case, there could only be one university, and many colleges enrolled in that.

The relation represents Java class by making the University class hold the n number of College object. And opposite to this College class will hold only a single University object reference. Hence the classes would look like the class mentioned below.

public class University {
  private String universityId;
  private String universityName;
  private String address;
  private List<College> colleges;
}

public class College {
  private String collegeId;
  private String collegeName;
  private University university;
}

The Enumerations can be used to associate the entities with others. It provides the ability to choose a value from a defined set of values. When there are a minimal set of choices, enums are the suitable choice.

In the below class diagram, check the aggregation and composition types of relationships.

composition and aggregation

The black dark diamond symbol represents the type of relationship to be composition. In simpler terms, Composition is dependency over the two entities, or if one entity does not exist, then the other will also not exist. So the case is shown above. car entity is composed of the Wheels class. So without Wheels instance, a Car is not considered to be a car. So it depicts strong relation and hence Composition relation.

To achieve composition behavior in the classes below is an example.

class Wheel {
  private String manufacturer;
}

final class Car {
  private final Wheel Wheel; // Composition

  Car(Wheel wheel) {
    this.wheel = wheel;
  }

  public void move() {
    System.out.println("Car works");
  }
}
}

The final specifier with the Wheel instance in the Car class defines the Composition relation. And hence it must be initiated as and when a car instance gets called. The wheel gets instantiated in the constructor.

Another relationship is Aggregation and denoted by the black empty diamond shape. The two entities are not strongly related and have their independent existence, and hence is the opposite of the composition relation. If one of the either two is not present, then too other can survive. As stated above, with the Car and Brand entity. Even if a car has no Brand Name instance, then there’s the existence of a car class.

Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java UML