Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 39 ยท State and Component Diagrams

Composite Structure, Ports and Parts

Learn UML composite structure diagrams: parts, ports, connectors and collaborations to show the internal structure of a class or component, with Python examples.

A class diagram tells you which attributes a class has. It does not tell you how the objects inside an instance are wired together at run time. A composite structure diagram fills this gap. It opens up a class or component and shows its internal parts, the ports through which they talk and the connectors between them.

Think of a car. The class Car has an engine, a gearbox and wheels. The diagram shows that the engine connects to the gearbox, the gearbox to the drive shaft, and the drive shaft to two of the four wheels, not all of them. A plain aggregation arrow from Car to Wheel could not say that.

Parts, ports and connectors

Element Notation Meaning
Structured classifier Outer box with a name The class or component being opened up
Part Inner box, labeled name : Type A role an instance plays inside the whole; it is owned by the container
Port Small square on a border A named interaction point of the whole or of a part
Connector Line between parts or ports A communication link between two roles
Delegation connector Connector from an outer port to a part's port Forwards the outside world's messages to the part that handles them

A part is a role, not just a type. A Car may have four parts of type Wheel named frontLeft, frontRight, rearLeft, rearRight. They are the same class playing four different roles. Multiplicity can be added, for example wheels : Wheel [4].

Parts live and die with their owner: if the car instance is destroyed, its parts are destroyed too, which is composition semantics.

flowchart LR
    subgraph Car
        direction LR
        E["engine : Engine"] --> G["gearbox : Gearbox"]
        G --> D["driveShaft : Shaft"]
        D --> RL["rearLeft : Wheel"]
        D --> RR["rearRight : Wheel"]
    end
    Driver(["Driver"]) -->|"pedals port"| E

The structure shows wiring, which a class diagram cannot express. Only the rear wheels are driven.

Ports and interfaces

A port may offer a provided interface, require a required interface, or both. Ports make the whole object a black box: others connect to the port, never to the parts. A delegation connector passes a call received on an outer port to the correct inner part. If you later swap the engine for an electric motor, the outside world does not notice, provided the port contract is unchanged.

Ports can also be behavior ports, handled by the container itself rather than by a part. This is useful for a state machine of the whole: the container reacts directly to events arriving at the port.

Collaborations

A collaboration describes how a set of roles work together to achieve something, independent of which classes will play them. It is drawn like a composite structure with dashed ellipse notation and is the standard way to document a design pattern's structure. For example, Observer is a collaboration of the roles subject and observer; a collaboration use binds the roles to actual classes. You can then show that NewsFeed plays subject and Dashboard plays observer in one application.

Roles let you write pattern descriptions once and reuse them. See Design Patterns in UML for this applied.

When to use composite structure diagrams

Use them when:

  • The wiring of internal objects is the interesting part (embedded systems, hardware/software co-design, game engines, plugin hosts).
  • You have the same type playing several roles.
  • You want to define a clear boundary of ports on a component.

Skip them when a class diagram already says everything. Many teams use them rarely, but for components with complex internals they are the clearest option.

Parts and delegation in code

In code, a composite structure is usually a class whose constructor builds and wires the parts. The outer methods are delegation connectors.

Python A Car built from wired parts

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.

The caller only knows press_throttle. The wiring (engine to gearbox to two wheels) stays hidden, like a port hiding its parts.

Roles bound to classes

A collaboration binds roles to concrete classes. Here a generic Subject and Observer role, plus a binding that selects the classes.

Python Collaboration roles and a binding

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a composite structure diagram show that a class diagram does not?

    It opens a classifier and shows parts, ports and connectors.
  2. 2. A Car has four parts of type Wheel named frontLeft, frontRight, rearLeft and rearRight. What are these?

    A part is a role. One type can play several roles in one structure.
  3. 3. What is a delegation connector?

    Delegation hides parts behind the container's ports.
  4. 4. What happens to parts when the owning instance is destroyed?

    Parts are owned by the container and share its lifetime.

Summary

  • Composite structure diagrams open a class or component to show parts, ports and connectors.
  • A part is a role; the same type can play several roles.
  • Ports are named interaction points; delegation connectors forward outside requests to inner parts.
  • Collaborations describe role-based structures such as patterns and are bound to classes where used.
  • Use these diagrams when internal wiring matters; otherwise a class diagram is enough.

Next: Deployment Diagrams