Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 39 ยท State and Component Diagrams

Component Diagrams

Learn UML component diagrams: components, provided and required interfaces, ports and assembly connectors, with a runnable dependency checker in Python.

A class diagram shows the fine-grained structure of code. When a system has hundreds of classes, that view is too detailed to explain how the big pieces fit together. A component diagram zooms out. It shows the replaceable building blocks of a system, the interfaces they offer and need, and the wiring between them.

A component is a modular part of a system that encapsulates its contents and exposes them only through well-defined interfaces. In practice a component might be a library, a service, a plugin, a package of related classes or a whole microservice. The key idea is that someone who uses it does not need to know what is inside.

Notation

In UML 2.x a component is drawn as a rectangle with the keyword <<component>> or a small component icon (a box with two tabs) in the corner. Three notations describe its contract:

  • Provided interface (lollipop, a small circle on a stick): a service the component offers to others.
  • Required interface (socket, a half circle): a service the component needs from others.
  • Assembly connector: a lollipop plugged into a socket. It shows that one component's required interface is satisfied by another's provided interface.

Reading a component diagram means following the plugs: "OrderService requires PaymentGateway; PaymentAdapter provides it."

Mermaid has no dedicated component diagram, but a flowchart with labeled boxes and dashed arrows expresses the same idea. The arrow points from the client to the interface it uses.

flowchart LR
    subgraph Web["Web Shop"]
        UI["Storefront UI"]
        ORD["Order Service"]
        PAY["Payment Adapter"]
        INV["Inventory Service"]
    end
    UI -->|"IOrders"| ORD
    ORD -->|"IPayment"| PAY
    ORD -->|"IStock"| INV
    PAY -.->|"HTTPS"| EXT["External Payment Provider"]

Each label names the interface that the arrow's target provides.

Ports

A port is a named interaction point on the boundary of a component. It groups related interfaces: a port named admin might provide IConfig while a port named data requires IDatabase. Ports matter because they let a component be used in two roles without confusion, and they are the connection point for composite structure diagrams.

Dependency and the direction of arrows

A dashed arrow with an open head is a dependency: "A uses B, so a change to B may affect A". Good component design makes dependencies point from volatile parts toward stable ones, which is the dependency inversion principle. Two warning signs:

  1. Cycles. If Orders depends on Billing and Billing depends on Orders, neither can be built, tested or replaced alone.
  2. Hub components. One component that everything depends on becomes a bottleneck for change.

You can detect cycles from the diagram, but a small script does it better when the diagram is large.

What belongs inside a component

You can show internals by nesting parts in the component box, or leave them hidden (a black-box view). A white-box view lists the classes that realize the component. Use black-box views in overview diagrams and white-box views only where needed. A realization is a dashed line with a hollow triangle from the class to the component.

Granularity: how big is a component?

There is no fixed size, but useful rules of thumb exist:

  • It has one clear responsibility that you can name in a few words ("Billing", "Search").
  • It can be built, tested and deployed on its own, or at least replaced without touching the others.
  • Its interface is much smaller than its implementation.
  • A team can own it.

If you cannot name the interface in a sentence, the component is probably two things glued together.

Components versus packages

A package is a namespace for organizing model elements. It has no run-time meaning. A component is a run-time or build-time unit with interfaces. In small systems they often match, but you can have many packages inside one component.

A small component model in code

The first example describes components with provided and required interfaces, then checks that every required interface is satisfied and finds the wiring.

Python Wire components by interfaces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If you delete PaymentAdapter and run again, OrderService shows an unsatisfied interface: that is exactly the kind of gap a component diagram should reveal during design.

Detecting dependency cycles

Python Find cycles between components

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fix it by introducing an interface or an event: for instance, Customers could publish a CustomerChanged event that Orders listens for, so Customers no longer depends on Orders directly.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the lollipop and socket notation, what does a socket mean?

    A lollipop is provided, a socket is required. Plugging them together forms an assembly connector.
  2. 2. Which is the best description of a component?

    Encapsulation and replaceability through interfaces define a component.
  3. 3. Orders depends on Billing and Billing depends on Orders. What is the problem?

    Cycles couple the components tightly. Break them with an interface or events.
  4. 4. How does a component differ from a package?

    Packages organize model elements; components are functional building blocks.

Summary

  • Component diagrams show replaceable building blocks and how they connect through interfaces.
  • Lollipops are provided interfaces, sockets are required interfaces, and a plug in a socket is an assembly connector.
  • Ports group interfaces on a component boundary.
  • Arrows are dependencies; keep them pointing toward stable parts and free of cycles.
  • Show black-box views in overviews and white-box views only when the internals matter.

Next: Composite Structure, Ports and Parts