UML Component Diagrams

Complete guide to UML Component Diagrams: learn how to model system components, interfaces, ports, and dependencies. Master component-based architecture design and documentation.

Table of Contents

1. What are Component Diagrams?

Component diagrams model the physical or logical components of a system and their dependencies. They show how components are organized, what interfaces they provide and require, and how they interact.

Component diagrams are used to:

  • Model component-based architecture
  • Design system structure at a high level
  • Document component interfaces and dependencies
  • Plan system integration
  • Show how components are deployed

Components represent modular, replaceable parts of a system that encapsulate implementation and provide a set of interfaces. They are larger than classes and represent physical or logical modules.

2. Components

2.1 What is a Component?

A component is a modular, replaceable part of a system that encapsulates implementation and provides a set of interfaces. Components can be:

  • Physical: Executable files, libraries, DLLs, JARs
  • Logical: Business components, service components, modules

2.2 Component Notation

Components are shown as rectangles with the <> stereotype or with a component icon:

@startuml component OrderService note right of OrderService Components are shown with <> stereotype end note @enduml

2.3 Component Characteristics

  • Encapsulated: Hides internal implementation
  • Replaceable: Can be substituted with another implementation
  • Modular: Self-contained unit
  • Interfaced: Provides and requires interfaces
  • 2.4 Component Types

    • Service Components: Provide business services
    • Entity Components: Represent business entities
    • Process Components: Handle business processes
    • Utility Components: Provide common utilities

    3. Interfaces

    3.1 Provided Interfaces

    A provided interface (also called export interface) defines services that a component offers to other components. Shown as a "lollipop" (circle on a stick).

    @startuml component OrderService interface IOrderService OrderService --( IOrderService note right of IOrderService Provided interface shown as "lollipop" end note @enduml

    3.2 Required Interfaces

    A required interface defines services that a component needs from other components. Shown as a "socket" (semicircle).

    @startuml component OrderService interface IPaymentService OrderService )-- IPaymentService note right of IPaymentService Required interface shown as "socket" end note @enduml

    3.3 Interface Notation

    Interfaces can also be shown as classes with the <> stereotype:

    @startuml component OrderService interface IOrderService OrderService ..> IOrderService : uses @enduml

    3.4 Interface Contracts

    Interfaces define contracts:

    • Provided interfaces: What the component offers
    • Required interfaces: What the component needs
    • Contract: Both sides must match for components to work together

    4. Ports

    4.1 What are Ports?

    A port (UML 2.0+) is an interaction point between a component and its environment. Ports allow components to specify exactly how they interact with other components.

    4.2 Port Notation

    Ports are shown as small squares on the component boundary:

    @startuml component OrderService { portin port1 portout port2 } note right of OrderService Ports shown as small squares on component boundary end note @enduml

    4.3 Ports with Interfaces

    Ports can be connected to interfaces:

    @startuml component OrderService { portout port1 portin port2 } interface IProvided interface IRequired port1 --( IProvided IRequired )-- port2 note right of OrderService Ports connected to provided and required interfaces end note @enduml

    4.4 Using Ports

    • Ports provide a cleaner way to show component interactions
    • They make it clear where components connect
    • They support more complex interaction patterns
    • They're particularly useful for large, complex systems

    5. Dependencies and Relationships

    5.1 Dependency

    A dependency shows that one component depends on another. Shown as a dashed arrow.

    @startuml component OrderService component PaymentService OrderService ..> PaymentService @enduml

    5.2 Interface Dependency

    Components depend on interfaces, not implementations:

    @startuml component OrderService interface IPaymentService OrderService ..> IPaymentService @enduml

    5.3 Assembly Connector

    An assembly connector connects a provided interface to a required interface:

    @startuml component OrderService component PaymentService interface IPaymentService OrderService --( IPaymentService IPaymentService )-- PaymentService @enduml

    5.4 Delegation Connector

    A delegation connector connects a component's port to an internal part:

    @startuml component Component { component InternalPart portin port1 port1 --> InternalPart } @enduml

    5.5 Realization

    A component realizes (implements) interfaces:

    @startuml component OrderService interface IOrderService OrderService ..|> IOrderService @enduml

    6. Best Practices

    6.1 Define Clear Components

    • Each component should have a single, well-defined responsibility
    • Components should be cohesive (related functionality together)
    • Components should be loosely coupled (minimal dependencies)
    • Use meaningful, descriptive component names

    6.2 Design Good Interfaces

    • Define clear, stable interfaces
    • Minimize the number of interfaces per component
    • Keep interfaces focused and cohesive
    • Document interface contracts clearly

    6.3 Manage Dependencies

    • Minimize dependencies between components
    • Avoid circular dependencies
    • Depend on interfaces, not implementations
    • Use dependency injection for flexibility

    6.4 Organize Components

    • Group related components together
    • Use packages or subsystems for large systems
    • Show different levels of abstraction
    • Keep diagrams readable and uncluttered

    6.5 Show Appropriate Detail

    • Show only relevant components and dependencies
    • Use different diagrams for different views
    • Hide implementation details when not needed
    • Focus on architecture, not implementation

    7. Practical Examples

    7.1 E-Commerce System Components

    @startuml component OrderService component PaymentService component InventoryService interface IPaymentService OrderService --( IPaymentService IPaymentService )-- PaymentService OrderService ..> InventoryService PaymentService ..> InventoryService @enduml

    7.2 Web Application Architecture

    @startuml component "Web Controller" as WebController component UserService component "Database Component" as Database interface IUserService WebController --( IUserService IUserService )-- UserService UserService ..> Database @enduml

    7.3 Microservices Architecture

    @startuml component "Order Microservice" as OrderMS component "Payment Microservice" as PaymentMS component "API Gateway" as APIGateway OrderMS ..> APIGateway PaymentMS ..> APIGateway OrderMS ..> PaymentMS @enduml

    8. Conclusion

    Component diagrams are essential for modeling component-based architectures and designing modular, maintainable systems. They help visualize system structure, component interfaces, and dependencies at a high level.

    Effective component modeling requires understanding components, interfaces, ports, and relationships. Components should be well-defined, loosely coupled, and depend on interfaces rather than implementations.

    Next Topic: Deployment Diagrams - Learn how to model physical deployment of software to hardware nodes.

    Post a Comment

    0 Comments