UML Class Diagrams help us understand how software works. They show the parts of a software system, what each part does, and how these parts connect with each other. This tutorial makes it easy to learn about UML class diagrams. It's great for both beginners and those who already know some things about software. We will look at what these diagrams are, the symbols they use, and how to read them. This guide is very useful for anyone who wants to get better at making software.
In these diagrams, we see how different parts of a system work together. This tutorial explains the different ways parts can be connected, like association, aggregation, and inheritance. We use easy examples to show these ideas. This helps anyone who is planning a new software project or trying to understand an existing one. This guide will teach you how to make UML class diagrams in a simple way.
In this article
Part 1. What is a UML Class Diagram
A class diagram is one of the most widely used types of UML diagrams in software engineering. Class diagrams are important as they give a visual representation of how a computer program works and details components of a computer program along with their properties and methods. Below are the main components of a UML Class Diagram:
Class:
Represents a blueprint or template for creating objects.
Typically depicted as a rectangle with three compartments: the top one for the class name, the middle one for attributes, and the bottom one for methods or operations.
Attributes:
Represent the properties or characteristics of a class. Attributes are generally listed in the middle compartment of the class rectangle. Each attribute has a name and a type.
Operations/Methods:
Represent the functions or actions that can be performed by the objects of a class. Listed in the bottom compartment of the class rectangle. Each operation has a name, parameters, and a return type.
Relationships:
The relationship between classes in UML refers to the association or connection established between different classes within a system. These relationships define how classes interact with each other, exchange information, and contribute to the overall functionality of the system.
Part 2. Class Diagram Notations and Symbols
UML diagrams make use of specific shapes to specify objects. Below we are going to see how classes are represented with a help of a Person class and its relation with Address.
▪ Representing a Class
A class is a basic component in a UML class diagram. A class is represented by a rectangle. The name of the class comes on the top of the rectangle with a separator below. Below is a representation of a Person class-diagram
▪ Class Attributes
Now that we have a class shape with the name of the class on the top, time to add the attributes of the class. The attributes are properties of class. Remember that class is a blue print. So any instance of the class will have its own set of attributes.
Attributes are shown as in the second partition below the title of the class. Attribute names are shown with the attribute type following a colon. These attributes correspond to member variables (data members) in the code.
So a Person instance can have these attributes for example: Name, Data of Birth, Gender, Address and so on. Below is how a Person instance can be represented using UML shape:
▪ Methods
Every class is supposed to perform in some way and fulfill a specific functionality. These methods are specified in the class shape below the attributes partition. So suppose our Person can perform a daily routine then the method performDailyRoutine could be added in the class shape as:
Notice that if the method returns any value then it is shown by type following a colon.
▪ Visibility
In object programming languages, it is possible to alter the visibility of both the attributes and methods so that they can either be exposed to the outer world, within inheritance or just visible within the class.
- Public: It is represented by (+)
- Private: It is represented by (-)
- Protected: It is represented by (#)
Now that we know how to define classes in the UML class diagram, now let’s see what kind of information is really needed to be shown in the UML diagram.
Part 3. Relationships Between Classes
In UML diagrams relationships between the classes are expressed via arrow.
Inheritance
This is probably the most used relationship in the UML. It can be defined as the:
Connection between entirety and its components such that the components derive the properties from their parents.
In the context of UML (Unified Modeling Language), inheritance, often termed generalization, delineates the connection between a superclass and its subclass. A superclass is alternatively named a base class, while the subclass is referred to as a derived class. The subclass is capable of incorporating additional information alongside the inherited elements from the superclass.
Composition
Composition can be briefly explained as:
The connection between the entirety and its components is such that they are inseparable.
Below is an example of how Composition looks in the UML diagram:
It is read as: The computer class is composed of both input and output devices.
Aggregation
Aggregation indicates a weaker relationship than composition, where one class is associated with another class as a part, but the part can exist independently.
The connection between the entirety and its components allows for separation between the whole and its parts.
Now Student depends upon Semester and Address information. However, Student can still exist without having these pieces of information. Any instance of Student can have Semester or Address or neither yet can still exist.
Association
Association is another relationship used in UML diagrams. Represents a generic relationship between classes, indicating that they are somehow connected or interact with each other.
In the UML context, a class property is responsible for maintaining a reference to an instance or multiple instances of another class.
Dependencies
- A dependency connection signifies a "use" relationship, signifying that alterations in one entity can impact others relying on it. The term "dependency" is employed when it's essential to denote that one entity utilizes another.
Interface/Implementation
Interface is how we change or modify the behavior of one class into another. Here we do not need to inherit every attribute from the base class. Rather we implement an interface that gives any class necessary adoption.
Signifies the implementation of an interface or the realization of an abstract class by another class.
Perspectives of a Class Diagrams
Not always we show all the information in the UML class diagram. From need to need and case to case details of a class can be shown in the diagram.
Broadly there can be three ways how UML class diagrams can be created:
- Conceptual: This is only concerned with the main aspects of the business.
- Specification: This is more detailed than a conceptual UML class diagram and here we show the details of a class in the diagram.
- Implementation: This is rather a detailed way of creating a UML class diagram.
Notice that the Person class depends upon another class called Address. This is where relationships come in the UML diagrams. This is what we are going to look next.
Part 4. UML Diagram Examples
Order System UML Class Diagram
1.This class diagram fulfills the purpose of placing an order at any shopping store and then checking it out.
2.There are essentially eight types of classes being:
① Customer: A customer consists of name and address. A customer places an order in the system and checks it out. A customer can place no, one, or multiple orders
② Order: An order defines the action of customer at the store. Order comprises of date, status of the order and can calculate totals and tax. An order must contain one or more order details.
③ Order Detail: Every order consists of multiple lines called Order Detail. Here we define the quantity and taxStatus of that line. One order detail instance can have only have one item.
④ Item: For each order line there is an item. An item can have shipping weight and description.
⑤ Payment: In our system a customer can place and order comprising of multiple items in the order. These make up the order details and then a customer can checkout via Payment. Three types of payments can happen each having their own attributes.:Cash、Check and Credit.
UML Diagram of Super Market:
1. It can be used to show the process of any retail business including supermarkets or online ecommerce stores.
2. The use case it solves for any retail business is how to make the billing.
3. There are essentially five classes:
① Supermarket Billing: This is the heart of the UML diagram where actually the functionality takes place.
② Customers: A customer is billed against a purchase.
③ Purchase: A purchase is what the Customer does in the supermarket. The purchase depends upon another entity called Rates.
④ Rates: Here we define the pricing of different items in the our market place
⑤ Inventory: The inventory is updated every time a billing happens in our shopping store.
Part 5. How to Draw a Class Diagram
Drawing a class diagram involves representing the structure of a system by illustrating its classes, attributes, methods, and relationships.
No matter which software you use or which whether you are drawing manually or not, the steps and concepts are generally going to remain the same.
Here's a step-by-step guide:
- Identify Classes:
- Identify the main classes in your system. These are typically the nouns in your problem domain.
- Consider what attributes (data) and methods (functions) each class might have.
- Create Class Boxes:
- Draw a box for each class. Place the class name at the top of the box.
- Add Attributes and Methods:
- Inside each class box, list the attributes and methods.
- Attributes are usually shown with their data types.
- Methods are listed with their parameters and return types.
- Draw Relationships:
- Identify relationships between classes. Common relationships include association, aggregation, and composition.
- Use lines with arrowheads to represent these relationships.
- Label the lines with multiplicity (e.g., 1, *, 0..1) to indicate how many objects are involved.
- Consider Multiplicity:
- Define how many instances of one class are related to an instance of another class. Multiplicity is often represented near the ends of association lines.
- Include Inheritance:
- If your system involves inheritance (i.e., a class inheriting from another), represent it using an arrow with a hollow triangle pointing to the superclass.
- Consider Visibility:
- Optionally, you can use symbols like '+' (public), '-' (private), and '#' (protected) to denote the visibility of attributes and methods.
- Review and Refine:
- Ensure that your class diagram accurately reflects the relationships and structure of your system.
- Refine as needed based on feedback or changes in requirements.
Part 6. Conclusion
UML use case diagrams are indispensable in system modeling and design. They enhance communication, improve system understanding, and support effective project management. Following this comprehensive guide, you can harness the power of use case diagrams and apply them to various domains, ensuring your projects are well-structured and successful.