UML (Unified Modeling Language)
The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system. It helps to visually represent the architecture, design, and implementation of a system.
The various types of important UML diagrams are –
The most important UML diagram for Object-Oriented Design interviews is the Class Diagram. Let’s look at and discuss the diagram in more detail.
Class Diagram
The Class Diagram in the Unified Modeling Language (UML) is the most useful and important diagram that is being focused on in a low-level design interview. It is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects.
In the diagram, classes are represented with boxes that contain three compartments:
- The top compartment contains the name of the class. It is printed in bold and centered, and the first letter is capitalized.
- The middle compartment contains the attributes of the class. They are left-aligned and the first letter is lowercase.
- The bottom compartment contains the operations the class can execute. They are also left-aligned and the first letter is lowercase.
Relationships
The Class Diagram shows the classes, their attributes, methods, and the relationships between different classes. Several types of relationships can be represented in class diagrams, each indicating a different type of association between classes. Here are some of the most common relationships – Association An association represents a relationship between two classes, where objects of one class are related to objects of the other class in some way. The relationship can be bi-directional or unidirectional. In a bi-directional relationship, both classes are aware of each other and their relationship. In a unidirectional relationship, two classes are related, but only one knows about the other and can call it. For example, consider the classes “Car” and “Driver”. There is an association between these classes because a car is driven by a driver. The association can be represented with an arrow pointing from the car class to the driver class, indicating the direction of the relationship.
Aggregation is a special type of association that represents a “has-a” relationship between classes. It indicates that one class as a “whole” contains objects of another class as a “part” of its state. The “part” class can exist independently without the “whole” class. It is represented by the diamond symbol. For example, consider the classes “University” and “Department”. A university has several departments, and a department belongs to one university. This can be represented using an aggregation relationship, with a diamond symbol on the side of the university class pointing to the department class.
It is similar to aggregation, but it represents a stronger “whole-part” relationship between classes. It indicates that one class is made up of one or more objects of another class, and those objects cannot exist without the parent class. For example, consider the classes “House” and “Room”. A house is composed of one or more rooms, and a room cannot exist without a house. This can be represented using a filled diamond symbol on the side of the house class pointing to the room class.
Inheritance represents an “is-a” relationship between classes, where one class inherits the attributes and methods of another class. The inherited class is called the superclass or parent class, and the inheriting class is called the subclass or child class. For example, consider the classes “Animal” and “Cat”. A cat is an animal, so the class “Cat” can inherit from the class “Animal”. This can be represented using an arrow pointing from the subclass to the superclass, with a hollow triangle symbol on the side of the superclass.
A realization relationship is a relationship between two entities, in which one class implements the behavior of the other class or interface. It is represented as a hollow triangle shape on the interface end of the dashed line that connects it to one or more implementers. For example, consider an interface “Shape” which has the draw() method. The classes such as “Rectangle” and “Circle” implement draw() method of Shape and provide their implementation like how a Rectangle or Circle can be drawn.
Dependency represents a weaker relationship between classes, where one class depends on another class in some way. This can be because the dependent class uses objects or methods of the other class, or because it receives parameters or returns values from the other class. For example, consider the classes “BillingSystem” and “Customer”. The billing system depends on the customer class to get information about customers’ orders, billing addresses, etc. This can be represented with a dashed arrow pointing from the billing system class to the customer class.
Multiplicity refers to the number of instances of one class that can be associated with the instances of another class. It specifies the range of valid cardinalities for a given association between classes. It is represented by two integers separated which tells a range. The first integer represents the minimum number of instances of the related class that must be associated with an instance of the source class, while the second integer represents the maximum number of instances of the related class that can be associated with an instance of the source class. It can be applied to all UML relationships. For example, consider a relationship between the classes “Order” and “Item”, where an order can have one or more items, but an item can only be associated with one order. It is represented as 1..* (One to Many).
Other common multiplicity values include:
- 0..1 – Indicates that zero or one instance of the related class can be associated with an instance of the source class.
- 1..1 – Indicates that exactly one instance of the related class must be associated with an instance of the source class.
- 0..* – Indicates that zero or many instances of the related class can be associated with an instance of the source class.
For more such posts and courses please visit – TechReady