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.
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.
- For creating a class, go to the editor say
Intellij
IDE. - Create a new class named University, College, Students, and Department.
- Enter the fields with the datatype in the format as
private String university Id
type. - Select the newly created fields and right-click over the screen or press the Alt+Insert key.
- A
generate
pop-up will flash on the screen. - Scroll down to
Getter and Setter
- Click
Ok
at the bottom. - 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.
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 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