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:
DogandCatinherit fromAnimal. - Realization/Implementation:
DogandCatimplement thePetinterface. - Aggregation:
Personhas an aggregation relationship withPet, indicating that a person can have multiple pets. - Composition:
Personhas a composition relationship withAddress, indicating that an address cannot exist without a person. - Association:
Personhas an association relationship withPhone, indicating that a person can have multiple phone numbers. Dependency:Phonedepends on thePhoneTypeenumeration for thephoneTypeattribute.
- 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 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.
- 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 participantbefore 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--xarrow 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
Carhas aDriver. Car class uses, or referencesDriverclass.
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
Carclass has anEngineclass 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
Houseclass is composed ofRoomclass 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
Dogclass and aCatclass inherit from anAnimalclass, 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
Rectangleclass and aCircleclass implement theShapeinterface, 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
Customerclass uses anOrderclass 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!
