Assigning Responsibilities
In this pack:
sizing,
notation,
rule strictness.
Overview
Every behaviour needs an owner. This skill chooses the class that should
answer a request, create another object, or take on a new rule. It starts
from the information an object holds, falls back to a service only when
no domain object stays cohesive, and breaks ties with coupling and
cohesion. The result is a small set of classes that each have one reason
to change.
When to use
- A behaviour has no obvious class, or two classes could own it.
- A new method is about to land in a service, controller or helper.
- One class has grown methods for unrelated parts of the clinic.
- A request goes from the interface straight to a domain object, or the
interface holds a business condition.
- An object needs to be created and the owner of construction is
unclear.
- A design review needs class names, responsibilities and collaborators
before code.
When not to use
- The change fixes one method whose owner is not in question. Keep the
owner and change the body.
- The module is written as data and functions, not objects. Follow its
idiom and do not force objects onto it.
- The question is only where to put a line of formatting or logging,
and no domain rule is involved.
Core rules
Default. Give a behaviour to the object that holds the information
it needs. If a class already holds the values a decision reads,
the decision belongs there. Asking another object for those values
and deciding outside puts the rule and its data in different places,
where they drift apart.
The exception is a behaviour that would drag a database, a
framework or another context into the object. Give it to a
collaborator, as rule 4 describes.
Default. Create an object where its lifetime is controlled: in the
object that contains, records, or closely uses it, or in the object
that holds its initial data. A new object belongs where its
lifetime is managed and its data is at hand. A clinic creates its
schedule, the schedule creates its slots, and a booking use case
creates an appointment for a slot.
The exception is an object that must outlive its creator or cross a
module boundary. A factory owns construction in that case, while the
creator keeps the relationship.
Rule. Insert an intermediary when two objects must not know each
other. The intermediary exposes only the messages the caller needs
and translates them into the other object's language. It owns no
domain rule of its own; it only routes and adapts.
Default. Keep a behaviour in a domain object. A domain object
that holds the data usually has the best claim. A service is a last
resort, not the first place a new rule goes.
The exception is a rule that no domain object can hold without
losing cohesion. Put it in a service named after the clinic activity
it performs, such as booking or cancellation, not after a technical
layer or a pattern.
Rule. Send a request from a user interface or API first to a
use-case object that delegates. The use-case object names the
operation, loads what it needs, and asks domain objects to decide.
The interface does not call a domain object directly, and a business
condition does not stay in the interface layer.
Judgement. Keep each class to one or two roles, named in plain
words. A class that holds information may also structure other
objects, but it should not also coordinate a flow and talk to the
outside world. Name the role in a review: it holds information, it
structures other objects, it provides a service, it coordinates
others, it controls a flow, or it talks to the outside world. If a
class needs three roles, split it or move one role to a collaborator.
These are checks, not a fixed taxonomy; use the words that fit the
class.
Default. Give each class one reason to change: one kind of change
request. If two kinds keep landing on the same class, split the
class along the line between them. A method that changes for the same
reason as its neighbours stays.
The exception is two kinds of change that always arrive in the
same request. Splitting them would make every such request touch
two classes, so keep them together until they arrive apart.
Rule. Extract a class when one class knows and does too much.
Symptoms are a growing list of unrelated methods, groups of methods
that each use their own group of fields, and a name that needs "and"
to describe it. Move the
fields and methods that change together into a new class, give it a
domain name, and keep the original as a collaborator. The reference
on extracting a class starts from a ClinicManager that books,
reminds and invoices.
Default. Test a design before code with a responsibility card.
Write the class name, its responsibilities and its collaborators on
one card. If a responsibility needs a second card to explain, or a
collaborator is there only to fetch data, revise the design. The card
walkthrough for booking an appointment is in the Object roles
reference.
The exception is a change already excluded by the When not to use
section. Do not card a fix inside one method.
Judgement. Break a tie with coupling and cohesion. When two
objects could own a behaviour, pick the one that adds fewer
dependencies and keeps its class about one thing. Fewer dependencies
means fewer messages to other objects, fewer fields that exist only
for that behaviour, and fewer reasons for the class to change. If
both choices add the same dependencies, choose the object whose name
best matches the behaviour.
Pattern
Before, the use-case object CancelAppointment reads public fields
from Appointment and decides whether the booking can be cancelled.
After, Appointment owns that decision and the use-case object only
delegates.
Before:
class Slot
- patient: PatientId?
+ reserve(patientId: PatientId): void
raises SlotTaken
if patient = null then
patient := patientId
else
raise SlotTaken
+ release(patientId: PatientId): void
patient := null
class Appointment
+ slot: Slot
+ patient: PatientId
+ status: AppointmentStatus
+ cancellationReason: CancellationReason?
+ new(slot: Slot, patient: PatientId)
status := Scheduled
+ static book(slot: Slot, patient: PatientId): Appointment
slot.reserve(patient)
return new Appointment(slot, patient)
class CancelAppointment
+ run(appointment: Appointment, reason: CancellationReason): void
raises AppointmentNotCancellable
if appointment.status = Scheduled then
appointment.status := Cancelled
appointment.cancellationReason := reason
appointment.slot.release(appointment.patient)
else
raise AppointmentNotCancellable
return
After:
class Slot
- patient: PatientId?
+ reserve(patientId: PatientId): void
raises SlotTaken
if patient = null then
patient := patientId
else
raise SlotTaken
+ release(patientId: PatientId): void
patient := null
class Appointment
- slot: Slot
- patient: PatientId
- status: AppointmentStatus
- cancellationReason: CancellationReason?
- new(slot: Slot, patient: PatientId)
status := Scheduled
+ static book(slot: Slot, patient: PatientId): Appointment
slot.reserve(patient)
return new Appointment(slot, patient)
+ cancel(reason: CancellationReason): void
raises AppointmentNotCancellable
if status = Scheduled then
status := Cancelled
cancellationReason := reason
slot.release(patient)
else
raise AppointmentNotCancellable
return
class CancelAppointment
+ run(appointment: Appointment, reason: CancellationReason): void
appointment.cancel(reason)
return
Appointment.book reserves the slot, and the constructor sets the
status. Only cancel sets the reason and releases the slot. The
constructor is private, so book is the only way to create an
appointment. No field is read before it is set, and no step runs twice.
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.
- A method that reads two or more values from another object and then
decides with them.
- A class named Manager, Processor or Handler whose methods touch
unrelated parts of the domain.
- A request handler or controller that holds business conditions.
- A class whose version history shows changes for unrelated reasons.
- A constructor that takes many unrelated collaborators.
Common mistakes
- Treating the object with the information as the object with the
database row, then putting persistence into the domain object.
- Reading one reason to change as one method, then splitting a cohesive
class into fragments.
- Moving every behaviour into services and leaving domain objects as
data holders.
- Letting the use-case object accumulate business rules instead of
delegating them.
- Inventing a service when an existing domain object could own the
behaviour without losing cohesion.
Related skills
- oo-design sizes a task and defines the rule
labels.
Further reading
- Object roles
- Extracting a class
1---2name: oo-assigning-responsibilities3description: Use when deciding which class owns a behaviour, where a method lives, which object holds its data or creates another, or when a class does too much.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-assigning-responsibilities7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Assigning Responsibilities1213**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## Overview1920Every behaviour needs an owner. This skill chooses the class that should21answer a request, create another object, or take on a new rule. It starts22from the information an object holds, falls back to a service only when23no domain object stays cohesive, and breaks ties with coupling and24cohesion. The result is a small set of classes that each have one reason25to change.2627## When to use2829- A behaviour has no obvious class, or two classes could own it.30- A new method is about to land in a service, controller or helper.31- One class has grown methods for unrelated parts of the clinic.32- A request goes from the interface straight to a domain object, or the33 interface holds a business condition.34- An object needs to be created and the owner of construction is35 unclear.36- A design review needs class names, responsibilities and collaborators37 before code.3839## When not to use4041- The change fixes one method whose owner is not in question. Keep the42 owner and change the body.43- The module is written as data and functions, not objects. Follow its44 idiom and do not force objects onto it.45- The question is only where to put a line of formatting or logging,46 and no domain rule is involved.4748## Core rules49501. Default. **Give a behaviour to the object that holds the information51 it needs.** If a class already holds the values a decision reads,52 the decision belongs there. Asking another object for those values53 and deciding outside puts the rule and its data in different places,54 where they drift apart.5556 The exception is a behaviour that would drag a database, a57 framework or another context into the object. Give it to a58 collaborator, as rule 4 describes.59602. Default. **Create an object where its lifetime is controlled: in the61 object that contains, records, or closely uses it, or in the object62 that holds its initial data.** A new object belongs where its63 lifetime is managed and its data is at hand. A clinic creates its64 schedule, the schedule creates its slots, and a booking use case65 creates an appointment for a slot.6667 The exception is an object that must outlive its creator or cross a68 module boundary. A factory owns construction in that case, while the69 creator keeps the relationship.70713. Rule. **Insert an intermediary when two objects must not know each72 other.** The intermediary exposes only the messages the caller needs73 and translates them into the other object's language. It owns no74 domain rule of its own; it only routes and adapts.75764. Default. **Keep a behaviour in a domain object.** A domain object77 that holds the data usually has the best claim. A service is a last78 resort, not the first place a new rule goes.7980 The exception is a rule that no domain object can hold without81 losing cohesion. Put it in a service named after the clinic activity82 it performs, such as booking or cancellation, not after a technical83 layer or a pattern.84855. Rule. **Send a request from a user interface or API first to a86 use-case object that delegates.** The use-case object names the87 operation, loads what it needs, and asks domain objects to decide.88 The interface does not call a domain object directly, and a business89 condition does not stay in the interface layer.90916. Judgement. **Keep each class to one or two roles, named in plain92 words.** A class that holds information may also structure other93 objects, but it should not also coordinate a flow and talk to the94 outside world. Name the role in a review: it holds information, it95 structures other objects, it provides a service, it coordinates96 others, it controls a flow, or it talks to the outside world. If a97 class needs three roles, split it or move one role to a collaborator.98 These are checks, not a fixed taxonomy; use the words that fit the99 class.1001017. Default. **Give each class one reason to change: one kind of change102 request.** If two kinds keep landing on the same class, split the103 class along the line between them. A method that changes for the same104 reason as its neighbours stays.105106 The exception is two kinds of change that always arrive in the107 same request. Splitting them would make every such request touch108 two classes, so keep them together until they arrive apart.1091108. Rule. **Extract a class when one class knows and does too much.**111 Symptoms are a growing list of unrelated methods, groups of methods112 that each use their own group of fields, and a name that needs "and"113 to describe it. Move the114 fields and methods that change together into a new class, give it a115 domain name, and keep the original as a collaborator. The reference116 on extracting a class starts from a `ClinicManager` that books,117 reminds and invoices.1181199. Default. **Test a design before code with a responsibility card.**120 Write the class name, its responsibilities and its collaborators on121 one card. If a responsibility needs a second card to explain, or a122 collaborator is there only to fetch data, revise the design. The card123 walkthrough for booking an appointment is in the Object roles124 reference.125126 The exception is a change already excluded by the When not to use127 section. Do not card a fix inside one method.12812910. Judgement. **Break a tie with coupling and cohesion.** When two130 objects could own a behaviour, pick the one that adds fewer131 dependencies and keeps its class about one thing. Fewer dependencies132 means fewer messages to other objects, fewer fields that exist only133 for that behaviour, and fewer reasons for the class to change. If134 both choices add the same dependencies, choose the object whose name135 best matches the behaviour.136137## Pattern138139Before, the use-case object `CancelAppointment` reads public fields140from `Appointment` and decides whether the booking can be cancelled.141After, `Appointment` owns that decision and the use-case object only142delegates.143144Before:145146```text147class Slot148 - patient: PatientId?149 + reserve(patientId: PatientId): void150 raises SlotTaken151 if patient = null then152 patient := patientId153 else154 raise SlotTaken155 + release(patientId: PatientId): void156 patient := null157158class Appointment159 + slot: Slot160 + patient: PatientId161 + status: AppointmentStatus162 + cancellationReason: CancellationReason?163 + new(slot: Slot, patient: PatientId)164 status := Scheduled165 + static book(slot: Slot, patient: PatientId): Appointment166 slot.reserve(patient)167 return new Appointment(slot, patient)168169class CancelAppointment170 + run(appointment: Appointment, reason: CancellationReason): void171 raises AppointmentNotCancellable172 if appointment.status = Scheduled then173 appointment.status := Cancelled174 appointment.cancellationReason := reason175 appointment.slot.release(appointment.patient)176 else177 raise AppointmentNotCancellable178 return179```180181After:182183```text184class Slot185 - patient: PatientId?186 + reserve(patientId: PatientId): void187 raises SlotTaken188 if patient = null then189 patient := patientId190 else191 raise SlotTaken192 + release(patientId: PatientId): void193 patient := null194195class Appointment196 - slot: Slot197 - patient: PatientId198 - status: AppointmentStatus199 - cancellationReason: CancellationReason?200 - new(slot: Slot, patient: PatientId)201 status := Scheduled202 + static book(slot: Slot, patient: PatientId): Appointment203 slot.reserve(patient)204 return new Appointment(slot, patient)205 + cancel(reason: CancellationReason): void206 raises AppointmentNotCancellable207 if status = Scheduled then208 status := Cancelled209 cancellationReason := reason210 slot.release(patient)211 else212 raise AppointmentNotCancellable213 return214215class CancelAppointment216 + run(appointment: Appointment, reason: CancellationReason): void217 appointment.cancel(reason)218 return219```220221`Appointment.book` reserves the slot, and the constructor sets the222status. Only `cancel` sets the reason and releases the slot. The223constructor is private, so `book` is the only way to create an224appointment. No field is read before it is set, and no step runs twice.225226## Red flags227228Places to look, not verdicts. A red flag becomes a finding only with229evidence: a measurement, a reproducible bug, or a failing test.230231- A method that reads two or more values from another object and then232 decides with them.233- A class named Manager, Processor or Handler whose methods touch234 unrelated parts of the domain.235- A request handler or controller that holds business conditions.236- A class whose version history shows changes for unrelated reasons.237- A constructor that takes many unrelated collaborators.238239## Common mistakes240241- Treating the object with the information as the object with the242 database row, then putting persistence into the domain object.243- Reading one reason to change as one method, then splitting a cohesive244 class into fragments.245- Moving every behaviour into services and leaving domain objects as246 data holders.247- Letting the use-case object accumulate business rules instead of248 delegating them.249- Inventing a service when an existing domain object could own the250 behaviour without losing cohesion.251252## Related skills253254- [oo-design](../oo-design/SKILL.md) sizes a task and defines the rule255 labels.256257## Further reading258259- [Object roles](references/object-roles.md)260- [Extracting a class](references/extracting-a-class.md)