Patterns the Language Provides
In this pack:
sizing,
notation,
rule strictness.
Overview
A pattern that costs an interface and a class per case can be replaced
by a feature the language already offers, when the force behind the
pattern is small. A function value is a piece of behaviour a caller may
pass around and call; it answers one message, call(args), which runs
it and answers its result. A closed set lists every case in one place,
and exhaustive matching is a match over that set that handles each
case. The language's own iteration walks a collection through a
read-only view. The composition root builds the object graph, and a
container builds objects from its own configuration.
This skill keeps a pattern's classes where they carry state, several
operations or a name the staff use, and lets the language's feature
replace them where it does the same job.
When to use
- A strategy, command or template step is a class with one method and
no state, and a function value would do the same job.
- A caller walks a collection through a hand-written iterator class
with its own next and has-next messages.
- A visitor adds operations to a closed set of cases, and the language
checks that a match handles every case.
- A factory exists only to choose which implementation to wire, and the
composition root can make the same choice.
- A design review asks whether the language now provides a pattern the
module writes by hand.
When not to use
- The language has no function values, no exhaustive matching or no
built-in iteration for the case at hand: the class-based pattern
stays.
- The question is which pattern a problem calls for at all: that is
choosing a pattern, which
oo-choosing-a-pattern decides.
- The strategy or command carries state, several operations or a name
the staff use: it keeps its class, as the core rules say.
- The question is the syntax of a function value or a match in one
programming language: that is outside this skill.
Core rules
Default. Pass a function value where a strategy has one operation,
holds no state and has no name the staff use: the function value is
the strategy. There is no interface to implement and no class per
case, and the call sits where the choice is made. The exception is a
strategy with several operations, state of its own, or a name in the
clinic's language, such as the late-cancellation rule the staff discuss:
it keeps its interface and classes, as replacing conditionals with
polymorphism, rule 5, chooses a strategy.
Default. Pass a function value for an action that is only ever run
where it is made. The call sits on the line that needs the action,
so no class and no interface stand between the reader and the step.
The exception is an action that must be queued, stored, logged,
undone or retried: it stays a command object that carries its data.
Default. Pass the varying step of a fixed algorithm to the class
that runs the algorithm, as a function value, rather than extending
a base class that leaves one step open. The algorithm stays in one
method, and the step arrives through the constructor. The exception
is a group of steps that vary together or share the algorithm's
state: hold them as one object behind a small interface, as choosing
composition over inheritance, rule 1, does.
Rule. Use the language's own iteration instead of an iterator
class: hand callers a read-only view or a sequence the language can
walk, never a hand-written object with its own next and has-next
messages, and keep the collection itself private. An iterator
class repeats what the language already does, and its own cursor is a
second place to keep correct. A read-only view lets a caller read
the collection without changing it, as encapsulating state and
behaviour, rule 5, asks.
Judgement. Replace a visitor with a closed set of cases and
exhaustive matching where the language checks that every match
handles every case. A visitor is an object that adds an operation
to every case by being sent each case in turn. With a closed set, a
new operation is one match and no change to the cases, while adding
a case makes the compiler list every match to update. What tips the
balance is whether new operations or new cases arrive more often, as
replacing conditionals with polymorphism, rule 7, weighs it, and
whether the language checks exhaustiveness.
Rule. Let the composition root, written by hand, do a factory's
wiring work, and a container only where a framework builds its own
objects, as inverting dependencies, rule 4 and its exception,
allow. A container is a tool that builds and wires objects from
its own configuration, and no container reaches domain code, as rule
5 there says. Keep a factory only where it decides how a domain
object is born: a named factory that enforces a valid birth stays, as
constructing valid objects, rule 4, asks.
Rule. Follow the idiom the module already uses: where the module
works with function values, a function value is the pattern; where
it is built of classes the team reads, a class the team recognises
can stay. A feature that removes a pattern in one module is no
reason to rewrite another. Changing the idiom costs more than the
feature saves, and the reader pays it on every later change. A class
the team recognises is one with a name the staff use, the exception
rule 1 gives; a strategy with one operation, no state and no such
name is passed as a function value in a module built of classes too.
Pattern
Before, ReminderPlanner holds a ReminderTiming collaborator, an
interface with one operation, and DayBefore and TwoHoursBefore each
implement it without a field. After, the timing is a function value, and
ReminderSetup builds the planner where the object graph is wired.
Before:
interface ReminderTiming
+ dueAt(appointment: Appointment): DateTime
class DayBefore implements ReminderTiming
+ dueAt(appointment: Appointment): DateTime
return appointment.startsAt().minusDays(1)
class TwoHoursBefore implements ReminderTiming
+ dueAt(appointment: Appointment): DateTime
return appointment.startsAt().minusHours(2)
class ReminderPlanner
- timing: ReminderTiming
+ new(timing: ReminderTiming)
+ dueAt(appointment: Appointment): DateTime
return timing.dueAt(appointment)
After:
class ReminderPlanner
- timing: (Appointment) -> DateTime
+ new(timing: (Appointment) -> DateTime)
+ dueAt(appointment: Appointment): DateTime
return timing.call(appointment)
class ReminderSetup
+ dayBefore(): ReminderPlanner
return new ReminderPlanner((appointment: Appointment) ->
appointment.startsAt().minusDays(1))
Appointment gains no field here. startsAt() is the query the
crossing system boundaries skill adds to Appointment. minusDays(1)
and minusHours(2) are taken to answer the time that many days or hours
earlier, and 1 and 2 are taken to be the number of days and hours
those calls ask for. ReminderSetup stands for the composition root.
ReminderTiming, DayBefore and TwoHoursBefore disappear because
each held one operation, no state and no name the staff use. Both
fences store nothing, and each version answers one due time per call:
the Before sends one dueAt to its timing, and the After calls its
function value once inside ReminderPlanner.dueAt. Every method
answers a value and raises nothing. A second method,
twoHoursBefore(), passes
(appointment: Appointment) -> appointment.startsAt().minusHours(2)
the same way and is left out of the fence.
Red flags
Places to look, not verdicts. A red flag becomes a finding only with
evidence: a measurement, a reproducible bug, or a failing test.
- An interface with one method whose implementations hold no fields.
- A hand-written iterator class over a collection the language can
walk.
- A visitor in a language that checks exhaustive matching over a closed
set.
- A factory whose only job is to pick which implementation to wire.
- A base class that exists to leave one step of an algorithm open.
- A module that mixes function values and one-method classes for the
same kind of job.
Common mistakes
- Turning a strategy the staff name into an anonymous function.
- Replacing a command that must be undone or retried with a bare
function.
- Rewriting a class-based module in function values because the
language allows it.
- Matching on a set of cases the language does not close, so a new case
passes unnoticed.
- Moving a factory that enforces a valid birth into a container.
Related skills
Further reading
- Functions for small patterns
- Matching and iteration
1---2name: oo-patterns-the-language-provides3description: Use when strategy or command classes could be functions, when writing an iterator or visitor by hand, or when the language builds in the pattern.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-patterns-the-language-provides7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Patterns the Language Provides1213**In this pack:**14[sizing](../oo-design/SKILL.md#how-much-design-a-task-warrants),15[notation](../oo-design/references/notation.md),16[rule strictness](../oo-design/SKILL.md#how-strictly-a-rule-is-meant).1718## Overview1920A pattern that costs an interface and a class per case can be replaced21by a feature the language already offers, when the force behind the22pattern is small. A function value is a piece of behaviour a caller may23pass around and call; it answers one message, `call(args)`, which runs24it and answers its result. A closed set lists every case in one place,25and exhaustive matching is a match over that set that handles each26case. The language's own iteration walks a collection through a27read-only view. The composition root builds the object graph, and a28container builds objects from its own configuration.2930This skill keeps a pattern's classes where they carry state, several31operations or a name the staff use, and lets the language's feature32replace them where it does the same job.3334## When to use3536- A strategy, command or template step is a class with one method and37 no state, and a function value would do the same job.38- A caller walks a collection through a hand-written iterator class39 with its own next and has-next messages.40- A visitor adds operations to a closed set of cases, and the language41 checks that a match handles every case.42- A factory exists only to choose which implementation to wire, and the43 composition root can make the same choice.44- A design review asks whether the language now provides a pattern the45 module writes by hand.4647## When not to use4849- The language has no function values, no exhaustive matching or no50 built-in iteration for the case at hand: the class-based pattern51 stays.52- The question is which pattern a problem calls for at all: that is53 choosing a pattern, which54 [oo-choosing-a-pattern](../oo-choosing-a-pattern/SKILL.md) decides.55- The strategy or command carries state, several operations or a name56 the staff use: it keeps its class, as the core rules say.57- The question is the syntax of a function value or a match in one58 programming language: that is outside this skill.5960## Core rules61621. Default. **Pass a function value where a strategy has one operation,63 holds no state and has no name the staff use: the function value is64 the strategy.** There is no interface to implement and no class per65 case, and the call sits where the choice is made. The exception is a66 strategy with several operations, state of its own, or a name in the67 clinic's language, such as the late-cancellation rule the staff discuss:68 it keeps its interface and classes, as replacing conditionals with69 polymorphism, rule 5, chooses a strategy.70712. Default. **Pass a function value for an action that is only ever run72 where it is made.** The call sits on the line that needs the action,73 so no class and no interface stand between the reader and the step.74 The exception is an action that must be queued, stored, logged,75 undone or retried: it stays a command object that carries its data.76773. Default. **Pass the varying step of a fixed algorithm to the class78 that runs the algorithm, as a function value, rather than extending79 a base class that leaves one step open.** The algorithm stays in one80 method, and the step arrives through the constructor. The exception81 is a group of steps that vary together or share the algorithm's82 state: hold them as one object behind a small interface, as choosing83 composition over inheritance, rule 1, does.84854. Rule. **Use the language's own iteration instead of an iterator86 class: hand callers a read-only view or a sequence the language can87 walk, never a hand-written object with its own next and has-next88 messages, and keep the collection itself private.** An iterator89 class repeats what the language already does, and its own cursor is a90 second place to keep correct. A read-only view lets a caller read91 the collection without changing it, as encapsulating state and92 behaviour, rule 5, asks.93945. Judgement. **Replace a visitor with a closed set of cases and95 exhaustive matching where the language checks that every match96 handles every case.** A visitor is an object that adds an operation97 to every case by being sent each case in turn. With a closed set, a98 new operation is one match and no change to the cases, while adding99 a case makes the compiler list every match to update. What tips the100 balance is whether new operations or new cases arrive more often, as101 replacing conditionals with polymorphism, rule 7, weighs it, and102 whether the language checks exhaustiveness.1031046. Rule. **Let the composition root, written by hand, do a factory's105 wiring work, and a container only where a framework builds its own106 objects, as inverting dependencies, rule 4 and its exception,107 allow.** A container is a tool that builds and wires objects from108 its own configuration, and no container reaches domain code, as rule109 5 there says. Keep a factory only where it decides how a domain110 object is born: a named factory that enforces a valid birth stays, as111 constructing valid objects, rule 4, asks.1121137. Rule. **Follow the idiom the module already uses: where the module114 works with function values, a function value is the pattern; where115 it is built of classes the team reads, a class the team recognises116 can stay.** A feature that removes a pattern in one module is no117 reason to rewrite another. Changing the idiom costs more than the118 feature saves, and the reader pays it on every later change. A class119 the team recognises is one with a name the staff use, the exception120 rule 1 gives; a strategy with one operation, no state and no such121 name is passed as a function value in a module built of classes too.122123## Pattern124125Before, `ReminderPlanner` holds a `ReminderTiming` collaborator, an126interface with one operation, and `DayBefore` and `TwoHoursBefore` each127implement it without a field. After, the timing is a function value, and128`ReminderSetup` builds the planner where the object graph is wired.129130Before:131132```text133interface ReminderTiming134 + dueAt(appointment: Appointment): DateTime135136class DayBefore implements ReminderTiming137 + dueAt(appointment: Appointment): DateTime138 return appointment.startsAt().minusDays(1)139140class TwoHoursBefore implements ReminderTiming141 + dueAt(appointment: Appointment): DateTime142 return appointment.startsAt().minusHours(2)143144class ReminderPlanner145 - timing: ReminderTiming146 + new(timing: ReminderTiming)147 + dueAt(appointment: Appointment): DateTime148 return timing.dueAt(appointment)149```150151After:152153```text154class ReminderPlanner155 - timing: (Appointment) -> DateTime156 + new(timing: (Appointment) -> DateTime)157 + dueAt(appointment: Appointment): DateTime158 return timing.call(appointment)159160class ReminderSetup161 + dayBefore(): ReminderPlanner162 return new ReminderPlanner((appointment: Appointment) ->163 appointment.startsAt().minusDays(1))164```165166`Appointment` gains no field here. `startsAt()` is the query the167crossing system boundaries skill adds to `Appointment`. `minusDays(1)`168and `minusHours(2)` are taken to answer the time that many days or hours169earlier, and `1` and `2` are taken to be the number of days and hours170those calls ask for. `ReminderSetup` stands for the composition root.171`ReminderTiming`, `DayBefore` and `TwoHoursBefore` disappear because172each held one operation, no state and no name the staff use. Both173fences store nothing, and each version answers one due time per call:174the Before sends one `dueAt` to its timing, and the After calls its175function value once inside `ReminderPlanner.dueAt`. Every method176answers a value and raises nothing. A second method,177`twoHoursBefore()`, passes178`(appointment: Appointment) -> appointment.startsAt().minusHours(2)`179the same way and is left out of the fence.180181## Red flags182183Places to look, not verdicts. A red flag becomes a finding only with184evidence: a measurement, a reproducible bug, or a failing test.185186- An interface with one method whose implementations hold no fields.187- A hand-written iterator class over a collection the language can188 walk.189- A visitor in a language that checks exhaustive matching over a closed190 set.191- A factory whose only job is to pick which implementation to wire.192- A base class that exists to leave one step of an algorithm open.193- A module that mixes function values and one-method classes for the194 same kind of job.195196## Common mistakes197198- Turning a strategy the staff name into an anonymous function.199- Replacing a command that must be undone or retried with a bare200 function.201- Rewriting a class-based module in function values because the202 language allows it.203- Matching on a set of cases the language does not close, so a new case204 passes unnoticed.205- Moving a factory that enforces a valid birth into a container.206207## Related skills208209- [oo-design](../oo-design/SKILL.md) sizes a task and defines the rule210 labels.211- [oo-choosing-a-pattern](../oo-choosing-a-pattern/SKILL.md) decides212 whether a pattern is needed at all.213- [oo-replacing-conditionals-with-polymorphism](../oo-replacing-conditionals-with-polymorphism/SKILL.md)214 weighs subtypes against a closed set with exhaustive matching.215- [oo-choosing-composition-over-inheritance](../oo-choosing-composition-over-inheritance/SKILL.md)216 holds an object instead of extending a base class.217- [oo-inverting-dependencies](../oo-inverting-dependencies/SKILL.md)218 builds the object graph at the composition root.219- [oo-constructing-valid-objects](../oo-constructing-valid-objects/SKILL.md)220 gives a named factory its job and keeps it valid.221222## Further reading223224- [Functions for small patterns](references/functions-for-small-patterns.md)225- [Matching and iteration](references/matching-and-iteration.md)