Learning UML - Part One

I am here to begin the series related to UML tutorials. In the first part of the series, we will go through the what and why of UML and related concepts.

Let’s start now by putting fundamental question.

What is UML (Unified Modelling Language)?

UML is a standard visual language to model the blue prints of any software getting developed. It was developed way back in 1997 by a group called Object Management Group (OMG). Since then, it has become almost default language to visualize, construct, and document the relationships and flow in software systems. As per OMG, UML can be defined as-

The Unified Modeling Language (UML) is a graphical language for visualizing, specifying, constructing, and documenting the artifacts of a software-intensive system. The UML offers a standard way to write a system's blueprints, including conceptual things such as business processes and system functions as well as concrete things such as programming language statements, database schemas, and reusable software components.”

UML has a close relationship with Object Oriented Programming fundamentals and it has rich tools to model the objects and their relationships.

There are three main components of UML as following.

  • Object/Entity
  • Relationship
  • Diagrams

Relationships used in UML

Below are the fundamental concepts extensively used in UML.

  • Inheritance (Parent-Child relationship)
    Inheritance holds the IS-A relationships between objects.

UML Representation


For example, a relationship between Manager and an Employee.

  • Manager is an Employee
  • Developer is an Employee
  • Tester is an Employee

Code sample

  1. class Employee  
  2.     {  
  3.     }  
  4.     class Manager: Employee  
  5.     {  
  6.     }  
  7.     class Developer: Employee  
  8.     {  
  9.     }  
  10.     class Tester: Employee  
  11.     {  
  12.     }  
Association (HAS-A/ Uses relationship with No Owner)

Association holds the HAS A relationship between objects.

UML Representation


For example, relationship between Employee and Swipe Card or Employee and Cafeteria in an office.

  • Employee uses a swipe card to enter to office.
  • Employee uses cafeteria for his regular food.

Code sample

  1. class SwipeCard  
  2.     {  
  3.         public void DoSwipe (Employee emp)  
  4.         {  
  5.             //call Swipe logic to track login details  
  6.         }  
  7.     }  
  8.   
  9.     class Employee  
  10.     {  
  11.         public void Login (SwipeCard card)  
  12.         {  
  13.             card.DoSwipe(this);  
  14.         }  
  15.     }  
In the above code example, both Swipe Card and Employee use each other’s methods however both are independent and have their own object life cycle.
  • Aggregation (HAS-MANY with parent relationship/ Single Owner with Child objects Individual life)
    Aggregation specifies a whole/part relationship between the aggregate (whole) and component object (part). The component object may be accessed through other objects without the aggregate object. The aggregate object is not responsible in the lifecycle of the component object.

In UML, it’s displayed as an unfilled diamond and a solid line.

UML Representation


For example, a relationship between Managers and Developers or University and Professors.

  • Manager has many developers who work under him.
  • University has many professors.
  • Transport department has many drivers
  • Pond has many ducks
Code sample
  1. /// <summary>  
  2.     /// Parent Object  
  3.     /// </summary>  
  4.     class Manager  
  5.     {  
  6.         public List<Developer> devList = new List<Developer>();  
  7.     }  
  8.   
  9.     /// <summary>  
  10.     /// Child Object  
  11.     /// </summary>  
  12.     class Developer  
  13.     {  
  14.         public int EmpId { get; set; }  
  15.         public string EmpName { get; set; }  
  16.     }  
In the above code example, Manager is the parent or Aggregate object and Developer is the child object. Here, Developer has only one parent i.e. Manager; however Developer class can also be used independently as following. Hence child class has its own life cycle.
  1. Developer dev = new Developer();  
Composition (PART-OF relationship/ Single Owner with Child objects life depends on parent)

Composition specifies strong HAS-MANY or HAS-Ownership or Part-of relationships where the composite object has sole responsibility for the lifecycle (creation/destruction) of the component parts.

Composition is depicted as a filled diamond and a solid line.

UML Representation


For example, a relationships between Project Deliverables and Manager or University and Departments.

  • Manager has the ownership to complete the project on time.
  • Company has the responsibility to carry out audit in all its locations.
  • University has many departments.
  • Engine is a part of a vehicle.
  • Room is a part of house.

Code sample

  1. /// <summary>  
  2.     /// Parent Object  
  3.     /// </summary>  
  4.     class Manager  
  5.     {  
  6.         public int EmpId { get; set; }  
  7.         public List<Project> projectList = new List<Project>();  
  8.     }  
  9.   
  10.     /// <summary>  
  11.     /// Child/Part object  
  12.     /// </summary>  
  13.     class Project  
  14.     {  
  15.         public string ProjectCode { get; set; }  
  16.         public bool IsSuccessfullyDelievered { get; set; }  
  17.         public Project (Manager mgr)  
  18.         {  
  19.   
  20.         }  
  21.     }  
In the above code example, Manager is the parent or Composite object and the Project is the part object. Here, Project has only one parent i.e. Manager and Project class can’t be used independently without Manager’s reference and hence the life cycle of Project class is dependent on Manager.
  • Dependency
    Dependency is used to show the dependency between consumer and provider. Below are the examples where it can be used.
  • A client is dependent on Service.
  • A consumer is dependent on the supplier.

UML Representation


Conclusion

In this first article of the UML series, we looked what UML is and where it can be used. We went through the fundamental object relationships also that are used in UML.

Hope you liked the article. Look forward for your comments/suggestions.


Similar Articles