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 β
Symbol | Meaning | Example |
---|---|---|
+ | 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
andCat
inherit fromAnimal
. - Realization/Implementation:
Dog
andCat
implement thePet
interface. - Aggregation:
Person
has an aggregation relationship withPet
, indicating that a person can have multiple pets. - Composition:
Person
has a composition relationship withAddress
, indicating that an address cannot exist without a person. - Association:
Person
has an association relationship withPhone
, indicating that a person can have multiple phone numbers. Dependency:Phone
depends on thePhoneType
enumeration for thephoneType
attribute.
- Inheritance:
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.
- 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 withactivate
/deactivate
.
- 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.
- Reply / Return Message
- Meaning: Response to a previous call.
- Notation: Dashed line back to the caller.
- Mermaid:
-->>
from callee to caller.
- 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.
- 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).
- 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
.
- 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.
- 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 referencesDriver
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 anEngine
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 ofRoom
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 aCat
class inherit from anAnimal
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 aCircle
class implement theShape
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 anOrder
class to place order.
π Multiplicity Indicators β
Notation | Meaning |
---|---|
1 | Exactly one |
0..1 | Zero or one |
* or 0..* | Zero or more |
1..* | One or more |
2..5 | Between 2 and 5 |
3 | Exactly 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 β
- Keep it Simple: Don't over-complicate diagrams
- Focus on Key Relationships: Show the most important relationships
- Use Appropriate Diagrams: Choose the right diagram type for your purpose
- Consistent Naming: Use clear, consistent naming conventions
- Add Notes: Include explanatory notes for complex parts
- Version Control: Keep diagrams updated with code changes
β DON'T β
- Don't Show Everything: Avoid cluttered diagrams with every detail
- Don't Skip Relationships: Missing relationships can confuse readers
- Don't Use Wrong Notations: Stick to standard UML notation
- Don't Create Outdated Diagrams: Keep diagrams synchronized with code
- 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!