Skip to content

Todo --> fix the diagrams โ€‹

๐Ÿง‘โ€๐Ÿ’ป Author: RK ROY

UML Basics - Visual Modeling for System Design โ€‹

๐ŸŽฏ Overview โ€‹

UML (Unified Modeling Language) is a standardized visual modeling language used to specify, visualize, construct, and document software systems. It provides a common vocabulary and set of notations for creating diagrams that represent different aspects of a system.

๐ŸŽจ Why UML Matters in LLD โ€‹

  • Visual Communication: Clear representation of system structure and behavior
  • Design Documentation: Permanent record of design decisions
  • Team Collaboration: Common language for developers, architects, and stakeholders
  • Problem Identification: Spot design issues before implementation
  • Code Generation: Some tools can generate code from UML diagrams

๐Ÿ“š Types of UML Diagrams โ€‹

๐Ÿ—๏ธ Structural Diagrams โ€‹

1. Class Diagram โ€‹

Most important for LLD - Shows classes, their attributes, methods, and relationships.

Class Diagram Elements โ€‹

SymbolMeaningExample
+Public+play()
-Private-city: int
#Protected#specialMeow()
~Package~helper()
{abstract}Abstract class{abstract} Vehicle
<<interface>>Interface<<interface>> Drawable
{static}Static member{static} counter: int
  • The relationships between the classes are as follows:
    • Inheritance: Dog and Cat inherit from Animal.
    • Realization/Implementation: Dog and Cat implement the Pet interface.
    • Aggregation: Person has an aggregation relationship with Pet, indicating that a person can have multiple pets.
    • Composition:Person has a composition relationship with Address, indicating that an address cannot exist without a person.
    • Association: Person has an association relationship with Phone, indicating that a person can have multiple phone numbers. Dependency: Phone depends on the PhoneType enumeration for the phoneType attribute.

2. Object Diagram โ€‹

  • Shows instances of classes at a specific moment in time.

3. Component Diagram โ€‹

Shows how components are organized and their dependencies.

๐ŸŽญ Behavioral Diagrams โ€‹

1. Use Case Diagram โ€‹

  • Shows system functionality from user's perspective.
  • A Use Case Diagram is a visual representation of how different users (also called actors) interact with a system.
  • Itโ€™s like a birdโ€™s-eye view of the systemโ€™s functionality without getting into any code.
  • It answers "What can users do with this system?"

2. Sequence Diagram โ€‹

  • Shows how objects interact over time.
  • A sequence diagram is a type of UML (Unified Modeling Language) diagram that shows how objects in a system interact with each other, step by step.
  • It focuses on the order of messages exchanged between different components or actors to achieve a particular task or use case.
  • "Who is doing what and when?"
  • A Sequence Diagram in UML is used to represent the flow of messages between different objects or components over time.
  • It shows how objects interact through different types of messages.

๐ŸŒ Types of Messages in Sequence Diagrams : โ€‹

A message represents a communication between lifelines (objects/actors). Below are the commonly used UML message types with concise explanations and Mermaid examples.

  1. Synchronous Message (Call)
  • Meaning: Sender waits for the receiver to finish (blocking call).
  • Notation: Solid line with a filled arrowhead to the receiver, often followed by a reply.
  • Mermaid: Use ->> for call and -->> for reply. You can show activation with activate/deactivate.
  1. Asynchronous Message (Signal)
  • Meaning: Sender does not wait (non-blocking); control returns immediately.
  • Notation: In UML, an open arrow. In Mermaid, use -) for async messages.
  1. Reply / Return Message
  • Meaning: Response to a previous call.
  • Notation: Dashed line back to the caller.
  • Mermaid: -->> from callee to caller.
  1. Create Message
  • Meaning: Creates a new lifeline instance.
  • Notation: Arrow to the created lifelineโ€™s head; the lifeline starts at that point.
  • Mermaid: Use create participant before the first message to it.
  1. Destroy (Delete) Message
  • Meaning: Terminates a lifeline.
  • Notation: Message to an X at the end of the lifeline; no further messages after.
  • Mermaid: Use destroy participant (and optionally --x arrow for emphasis).
  1. Self Message (Recursive/Internal Call)
  • Meaning: An object sends a message to itself.
  • Notation: A U-shaped arrow back to the same lifeline.
  • Mermaid: Use A->>A.
  1. Found Message
  • Meaning: Message with unknown sender (arrives from outside the system boundary).
  • Notation: Arrow originating from a filled circle to a lifeline (conceptual in UML).
  • Mermaid: Not directly supported; approximate by using a dedicated External participant.
  1. Lost Message
  • Meaning: Message whose receiver is unknown or not modeled.
  • Notation: Arrow to a filled circle at the edge (conceptual in UML).
  • Mermaid: Not directly supported; approximate by sending to a placeholder participant.

Notes

  • Guarded/conditional flows (alt/opt), loops, and parallel blocks are interaction fragments, not message types, though they commonly wrap messages.
  • Activation bars represent time the receiver is active/processing; helpful but optional.

3. Activity Diagram โ€‹

  • Shows workflow and business processes.

4. State Machine Diagram โ€‹

  • Shows states of an object and transitions between states.

๐Ÿ”— Relationships in UML โ€‹

1. Association (Simple Association) โ€‹

  • General relationship between classes.
  • Association represents a "uses-a" relationship between two classes where one class uses or interacts with the other.
  • Example : A Car has aDriver. Car class uses, or references Driver class.

2. Aggregation (Has-a) โ€‹

  • "Part-of" relationship where parts can exist independently.
  • Aggregation represents a "has-a" relationship where one class (the whole) contains another class (the part), but the contained class can exist independently.
  • Example: A Car class has an Engine class but the Engine class can exist without the Car class.

3. Composition (Part-of) โ€‹

  • Strong ownership where parts cannot exist without the whole.

  • Composition represents a strong "has-a" relationship where the part cannot exist without the whole. If the whole is destroyed, the parts are also destroyed.

  • Example: A House class is composed of Room class but the Room class can not exist without the House class.

4. Inheritance (Is-a) โ€‹

  • Generalization/specialization relationship.
  • Inheritance (or Generalization) represents an "is-a" relationship where one class (subclass) inherits the attributes and methods of another class (superclass).
  • Example: A Dog class and a Cat class inherit from an Animal class, as both dogs and cats are animals.

5. Realization/Implementation โ€‹

  • Implementation of interface by a class.
  • Realization or implementation represents a relationship between a class and an interface, where the class implements the methods declared in the interface.
  • Example: A Rectangle class and a Circle class implement the Shape interface, which declares a getArea() method.

6. Dependency โ€‹

  • One class uses another class.
  • Dependency represents a "uses" relationship where a change in one class (the supplier) may affect the other class (the client).
  • Example: A Customer class uses an Order class to place order.

๐Ÿ“Š Multiplicity Indicators โ€‹

NotationMeaning
1Exactly one
0..1Zero or one
* or 0..*Zero or more
1..*One or more
2..5Between 2 and 5
3Exactly 3

Example with Multiplicity โ€‹

๐Ÿ› ๏ธ Practical UML Example: Order Management System โ€‹

Let's design a complete order management system using various UML diagrams.

Class Diagram โ€‹

Sequence Diagram: Order Processing โ€‹

State Diagram: Order Lifecycle โ€‹

๐ŸŽฏ Best Practices for UML โ€‹

โœ… DO โ€‹

  1. Keep it Simple: Don't over-complicate diagrams
  2. Focus on Key Relationships: Show the most important relationships
  3. Use Appropriate Diagrams: Choose the right diagram type for your purpose
  4. Consistent Naming: Use clear, consistent naming conventions
  5. Add Notes: Include explanatory notes for complex parts
  6. Version Control: Keep diagrams updated with code changes

โŒ DON'T โ€‹

  1. Don't Show Everything: Avoid cluttered diagrams with every detail
  2. Don't Skip Relationships: Missing relationships can confuse readers
  3. Don't Use Wrong Notations: Stick to standard UML notation
  4. Don't Create Outdated Diagrams: Keep diagrams synchronized with code
  5. Don't Ignore Your Audience: Create diagrams appropriate for your audience

๐Ÿ”ง Tools for UML โ€‹

Free Tools โ€‹

  • Draw.io (now diagrams.net): Web-based, simple to use
  • PlantUML: Text-based UML diagrams
  • Mermaid: Markdown-based diagrams (used in this guide)
  • UMLet: Simple UML tool
  • ArgoUML: Open source UML modeling tool

Commercial Tools โ€‹

  • Lucidchart: Professional diagramming
  • Visual Paradigm: Comprehensive UML suite
  • Enterprise Architect: Advanced modeling tool
  • MagicDraw: Professional UML tool
  • Visio: Microsoft's diagramming tool

๐Ÿ“š UML in Different Phases โ€‹

1. Requirements Analysis โ€‹

  • Use Case Diagrams: Capture functional requirements
  • Activity Diagrams: Model business processes

2. System Design โ€‹

  • Class Diagrams: Define system structure
  • Component Diagrams: Show system architecture
  • Sequence Diagrams: Design interactions

3. Implementation โ€‹

  • Class Diagrams: Code structure reference
  • Object Diagrams: Runtime instances

4. Testing โ€‹

  • State Diagrams: Test state transitions
  • Sequence Diagrams: Test interaction scenarios

๐ŸŽ“ Practice Exercises โ€‹

Exercise 1: Library Management System โ€‹

Create UML diagrams for a library system with:

  • Books, Members, Loans
  • Borrowing and returning processes
  • Late fee calculations

Exercise 2: ATM System โ€‹

Design UML diagrams for an ATM system including:

  • User authentication
  • Account operations (withdraw, deposit, balance inquiry)
  • Transaction processing

Exercise 3: Restaurant Ordering System โ€‹

Model a restaurant ordering system with:

  • Menu management
  • Order taking process
  • Payment processing
  • Kitchen workflow

๐Ÿ“ Summary โ€‹

UML provides a powerful visual language for software design that:

  • Standardizes Communication: Common vocabulary for development teams
  • Documents Design Decisions: Permanent record of system structure
  • Identifies Problems Early: Visual representation helps spot issues
  • Facilitates Planning: Clear roadmap for implementation
  • Supports Maintenance: Understanding existing systems

Key takeaways:

  • Use appropriate diagram types for different purposes
  • Focus on clarity and simplicity
  • Keep diagrams updated with code changes
  • Choose the right level of detail for your audience
  • Use standard UML notation consistently

Remember: UML is a tool to help you design better systems, not an end in itself. Use it when it adds value to your development process!