Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Modeling Practice

Design Patterns in UML

See how UML documents design patterns: class diagrams for structure, sequence diagrams for behavior, and collaborations for roles, with Strategy, Observer and Decorator.

Design patterns are named, reusable solutions to recurring design problems. UML is the usual language for describing them: a pattern is a small piece of structure (classes and relationships) plus a small piece of behavior (how the objects interact). Learning to read a pattern from its diagram lets you recognize it in a code base, discuss it in a design review and adapt it to your needs.

This lesson shows how to document three well-known patterns with UML, and how to avoid common modeling traps. For the code and trade-offs of each pattern, follow the links to the design patterns tutorial: Strategy, Observer and Decorator.

A pattern has two views

View UML diagram Answers
Structure Class diagram (or collaboration) Which roles exist and how are they related?
Behavior Sequence diagram In what order do they talk at run time?

Always read both. Two patterns can have almost the same class diagram (Strategy and State do) and differ only in who changes the collaborator and when.

Strategy

Intent: define a family of algorithms, make them interchangeable, and let the client choose.

classDiagram
    class Checkout {
        -PricingStrategy pricing
        +setPricing(PricingStrategy) void
        +total(items) double
    }
    class PricingStrategy {
        <<interface>>
        +price(items) double
    }
    class RegularPricing
    class SalePricing
    Checkout o-- PricingStrategy
    PricingStrategy <|.. RegularPricing
    PricingStrategy <|.. SalePricing

Reading the diagram: Checkout (the context) holds a reference to an interface and delegates work to it. The two concrete strategies realize the interface. The hollow diamond shows that the strategy is shared or injected, not owned.

Observer

Intent: when one object changes, notify all dependents automatically.

classDiagram
    class Subject {
        <<interface>>
        +attach(Observer) void
        +detach(Observer) void
        +notify() void
    }
    class Observer {
        <<interface>>
        +update() void
    }
    class Thermometer {
        -double temperature
        +setTemperature(t) void
    }
    class Display
    Subject <|.. Thermometer
    Observer <|.. Display
    Subject o-- "*" Observer
    Display ..> Thermometer : reads

The behavior is clearer in a sequence diagram: a change triggers one update call per observer.

sequenceDiagram
    participant T as Thermometer
    participant D1 as Display 1
    participant D2 as Display 2
    T->>T: setTemperature(21)
    T->>D1: update()
    D1->>T: getTemperature()
    T->>D2: update()
    D2->>T: getTemperature()

Decorator

Intent: add behavior to an object dynamically by wrapping it, with the same interface as the wrapped object.

classDiagram
    class Notifier {
        <<interface>>
        +send(message) void
    }
    class EmailNotifier
    class NotifierDecorator {
        -Notifier wrapped
        +send(message) void
    }
    class SmsDecorator
    class LogDecorator
    Notifier <|.. EmailNotifier
    Notifier <|.. NotifierDecorator
    NotifierDecorator o-- Notifier
    NotifierDecorator <|-- SmsDecorator
    NotifierDecorator <|-- LogDecorator

The signature of this pattern is the pair of arrows: the decorator is a Notifier (generalization) and has a Notifier (aggregation). Recognize that pair, and you recognize Decorator, Proxy and Composite (which differ in intent and multiplicity).

Roles versus classes

A pattern diagram describes roles (Subject, Observer), not final classes. When you apply a pattern, you map roles to your classes: Thermometer plays Subject, Display plays Observer. In UML this is a collaboration with role bindings, as in Composite Structure. In your project diagrams, indicate the pattern with a note or a stereotype such as <<observer>>, but do not draw role names as the class names.

Showing patterns in a project

  1. Draw the application classes with their real names.
  2. Mark the pattern role in a note or stereotype: <<strategy>> RegularPricing.
  3. Keep pattern details (like attach/detach) out of the main diagram unless the reader needs them.
  4. Link to the pattern description instead of redrawing it each time.

Patterns as data and as code

The code of a pattern matches its diagram one-to-one. The two examples show Strategy and Observer, with the roles marked in comments so you can map the diagram to the code line by line.

Python Strategy: context, interface, concrete strategies

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.

Python Observer: subject notifies observers

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.

Same diagram, different intent

Strategy and State have the same class diagram: a context delegating to an interface with several implementations. The difference is behavioral. In Strategy the client picks the algorithm and it rarely changes during the object's life. In State the states themselves switch the context from one implementation to another as events arrive. A state machine diagram should accompany State, and a note explaining the selection rule should accompany Strategy. Looking only at class diagrams, you could mistake one for the other: pair them with the behavior view.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a pattern need both a class diagram and a sequence diagram?

    Two patterns can share a structure and differ only in behavior.
  2. 2. Which pair of arrows is the signature of Decorator?

    The wrapper implements the interface and holds a reference to another object with the same interface.
  3. 3. What is a role in a pattern diagram?

    Roles are mapped to application classes by a collaboration or a note.
  4. 4. How do Strategy and State differ if their class diagrams look the same?

    The difference is who changes the collaborator and why.

Summary

  • A pattern is documented by a structure view (class diagram) and a behavior view (sequence diagram).
  • Strategy, Observer and Decorator each have a recognizable arrow signature.
  • Diagram roles are mapped to application classes; mark roles with notes or stereotypes.
  • Similar class diagrams can hide different intents, so read the behavior view too.
  • Code can mirror the diagram line by line: keep role names in comments while learning.

Next: Model Review and Common Mistakes