Role Diagrams: How This Course Draws a Pattern
A pattern's class diagram gives every box the same weight and hides which part changes and what calls what, so this course draws each pattern as a role diagram too: plain-word roles, numbered calls, and a dashed fence around the part that varies.
Apprentice · Software Development · OOP · Updated
After this lesson you can
- Read a role diagram: its roles, its numbered calls and the fence around what varies
- Match a role diagram's roles to the classes in a class diagram of the same design
Key takeaways
- A class diagram shows every class with the same weight; it doesn't say which part will change, or what calls what first.
- A role diagram names each box by the job it does, in plain words, with the textbook's name for that role in small type underneath.
- Its arrows are numbered in the order the calls happen, and each is labeled with a verb: asks, tells, wraps, is a kind of.
- A dashed fence goes around the part that varies, and a second version of the diagram puts the app's real names on the same roles.
Before this: Arrows: Is-a, Has-a and Uses, and Functions as Values.
Look up any design pattern in a book or online, and you'll find its class diagram: four or five boxes with names like Context, Strategy and ConcreteStrategyA, joined by arrows. You can read every arrow now, and the picture still doesn't tell you the two things that matter most about a pattern. Which part is going to change? And what happens first, and then what?
Here's the same gap at Pedal & Go. The owner's newest idea is a weekend discount: 20% off on Saturdays and Sundays. It's the kind of rule that changes (a holiday price next month, a student price after that), so the design keeps it apart from the rental. Its class diagram, though, draws the discount boxes exactly like every other box on the page. This lesson shows the second drawing this course uses for every pattern, built to show what a class diagram leaves out.
The discount, in code
A rental's total is now handed the day's discount, as a function: weekday takes nothing off, and weekend takes off 20%. Sam's e-bike, for three hours:
Rental sam = new Rental("Sam", new List<IRentable> { new EBike() }, 3);
Console.WriteLine( quot;Weekday: ${sam.Total(Weekday)}");
Console.WriteLine( quot;Weekend: ${sam.Total(Weekend)}");
int Weekday(int price) => price;
int Weekend(int price) => price * 80 / 100;
interface IRentable { string Name { get; } int Price(int hours); }
class EBike : IRentable { public string Name => "E-bike"; public int Price(int hours) => 15 * hours; }
class Rental
{
public string Rider { get; }
public List<IRentable> Items { get; }
public int Hours { get; }
public Rental(string rider, List<IRentable> items, int hours) { Rider = rider; Items = items; Hours = hours; }
public int Total(Func<int, int> discount) => discount(Items.Sum(item => item.Price(Hours)));
}
Weekday: $45
Weekend: $36
interface Rentable { String name(); int price(int hours); }
class EBike implements Rentable {
public String name() { return "E-bike"; }
public int price(int hours) { return 15 * hours; }
}
class Rental {
String rider;
List<Rentable> items;
int hours;
Rental(String rider, List<Rentable> items, int hours) { this.rider = rider; this.items = items; this.hours = hours; }
public int total(IntUnaryOperator discount) {
return discount.applyAsInt(items.stream().mapToInt(item -> item.price(hours)).sum());
}
}
void main() {
IntUnaryOperator weekday = price -> price;
IntUnaryOperator weekend = price -> price * 80 / 100;
Rental sam = new Rental("Sam", List.of(new EBike()), 3);
IO.println("Weekday: quot; + sam.total(weekday));
IO.println("Weekend: quot; + sam.total(weekend));
}
Weekday: $45
Weekend: $36
class EBike:
name = "E-bike"
def price(self, hours): return 15 * hours
class Rental:
def __init__(self, rider, items, hours):
self.rider = rider
self.items = items
self.hours = hours
def total(self, discount):
return discount(sum(item.price(self.hours) for item in self.items))
def weekday(price):
return price
def weekend(price):
return price * 80 // 100
sam = Rental("Sam", [EBike()], 3)
print(f"Weekday: ${sam.total(weekday)}")
print(f"Weekend: ${sam.total(weekend)}")
Weekday: $45
Weekend: $36
class EBike {
name = "E-bike";
price(hours) { return 15 * hours; }
}
class Rental {
constructor(rider, items, hours) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total(discount) {
return discount(this.items.reduce((sum, item) => sum + item.price(this.hours), 0));
}
}
const weekday = (price) => price;
const weekend = (price) => Math.floor(price * 80 / 100);
const sam = new Rental("Sam", [new EBike()], 3);
console.log(`Weekday: ${sam.total(weekday)}`);
console.log(`Weekend: ${sam.total(weekend)}`);
Weekday: $45
Weekend: $36
interface Rentable {
name: string;
price(hours: number): number;
}
class EBike implements Rentable {
name = "E-bike";
price(hours: number): number { return 15 * hours; }
}
class Rental {
rider: string;
items: Rentable[];
hours: number;
constructor(rider: string, items: Rentable[], hours: number) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total(discount: (price: number) => number): number {
return discount(this.items.reduce((sum, item) => sum + item.price(this.hours), 0));
}
}
const weekday = (price: number): number => price;
const weekend = (price: number): number => Math.floor(price * 80 / 100);
const sam = new Rental("Sam", [new EBike()], 3);
console.log(`Weekday: ${sam.total(weekday)}`);
console.log(`Weekend: ${sam.total(weekend)}`);
Weekday: $45
Weekend: $36
$45 with 20% off is $36. To keep the sample short, the e-bike here is on its own, without its lock or its parent class. Java's function type is IntUnaryOperator (a function from one int to another) and C#'s is Func<int, int>; whole-dollar division is // in Python and Math.floor in JavaScript and TypeScript.
What the class diagram leaves out
Drawn as a class diagram, the design has a Rental that uses a Discount, and two kinds of discount that keep its promise. (A class diagram draws a function that's handed in as an interface with one method; here it's apply(price: int): int.) Every box has the same thick outline. Nothing says that Weekday and Weekend are the part that will change, while Rental should never be edited again. Nothing says who calls whom, or in what order. And the names a textbook would use for these boxes, Context and Strategy, say nothing about a bike-rental desk.
The role diagram
So each pattern in this course gets a second drawing, a role diagram, made of four parts:
- Roles in plain words. Each rounded box is named for the job it does ("the rental's total", "today's discount"), with the textbook's name for that role in small type underneath ("context", "strategy").
- Numbered calls. The arrows are numbered in the order things happen, and each one is labeled with a verb: asks, tells, wraps, is a kind of.
- A dashed fence around the part that varies. This is the point of nearly every pattern: put the part that changes in one fenced-off place, so a change stays inside the fence. (The next chapter, The Ideas Behind Every Pattern, makes this an idea of its own.)
- The app's real names on a second version, so you can map each role to the code.

Read it the way you'd read a short story, following the numbers. The counter asks the rental what it costs today (1). The rental adds up its prices and asks today's discount what's left (2). Today's discount is one of the kinds inside the fence. Next month's holiday price is one more box inside the fence, and nothing outside it changes.
Same design, two drawings
Here are both drawings of the discount, side by side: the class diagram, and the role diagram's second version, with the code's real names on the same roles.

Each role is one or more classes:
| Role | Its textbook name | At the desk |
|---|---|---|
| the caller | client | the counter's code that asks for the total |
| the code that never changes | context | Rental and its total |
| one discount rule | strategy | Discount: a function that takes a price and gives one back |
| the kinds, inside the fence | concrete strategies | weekday, weekend |
This shape has a name, Strategy, and you'll meet it properly in Pass 1, when a bakery wants a new deal every week. Here it's only something to draw.
Which drawing, when
Use the class diagram for structure: what classes exist, what each one keeps, what code to write. Textbooks, documentation and college classes use it, and so does the exercise at the end of this chapter. Use the role diagram to explain why a design is shaped the way it is: what changes, and what stays put. Every pattern lesson in this course shows both, so you'll learn to read the textbook's picture and to know what it's for.
Try it. The owner adds a student price: $5 off any rental. On the role diagram, what changes, and what doesn't? (One new box inside the fence, "student: $5 off", with an "is a kind of" arrow to today's discount. Nothing outside the fence changes.)
Check yourself
2 questions to try on the lesson page.
So what
A role diagram puts the fence where the change will happen and numbers the calls in order, so you can see a pattern's purpose, not only its boxes. From Pass 1 on, every pattern is drawn this way.