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 doint 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!


CS Preparation Notes