Architecture and design
Introduit
6:03
Génie logiciel
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
Narration en anglais · sous-titres en anglais et en 中文 au choix
Dans cette leçon
| English | Sens |
|---|---|
| layers/ˈleɪəz/ | Distinct levels arranged above, below or within one another in a structure or system. |
| dependency rule/dɪˈpendənsi ruːl/ | règle de dépendance |
| coupling/ˈkʌplɪŋ/ | couplage |
| Cohesion/kəʊˈhiːʒn/ | how closely the parts inside one module belong together |
| interface/ˈɪntəfeɪs/ | The operations and contracts through which other code uses an object or component. |
| design pattern/dɪˈzaɪn ˈpætn/ | A named trade-off. |
| Strategy/ˈstrætədʒi/ | a design pattern that puts interchangeable algorithms behind one interface |
| Observer/ɒbˈzɜːvə/ | a design pattern in which objects subscribe to be told when another object changes |
| Repository/rɪˈpɒzɪtəri/ | a design pattern that hides data storage behind a collection-like interface |
| dependency inversion/dɪˈpendənsi ɪnˈvɜːʃn/ | high-level code depends on abstractions, not on low-level details |
| Instability/ɪnstəˈbɪlɪti/ | a package metric: outgoing dependencies ÷ (incoming + outgoing) |
| Law of Demeter/lɔː ɒv ˈdemiːtə/ | a design rule: call methods only on yourself, your fields, parameters and objects you create, not on objects they return |
Layers pointing inward
- BookShare has three layers 层: presentation (the web app), domain (loans, members, catalogue) and storage (the database).
- The dependency rule 依赖规则: dependencies point inward. The domain knows nothing about any framework, and that is what makes it testable.

The dependency rule points —
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Domain purity is what makes it testable without browser or database.
Coupling and cohesion, counted
- Cohesion 内聚 keeps related changes in one module; coupling 耦合 spreads them. Changing the loan-limit rule touches 1 module and 2 files (high cohesion, low coupling).
- The same rule spread into the presentation layer touches 3 layers and 5 files. The count is the design measure.
- The borrow sequence: presentation calls
domain.borrow(member, copy); the domain checks the limit and the copy's status; storage records the loan with its dates; presentation confirms. Four participants, five labelled messages.
The loan-rule change touches 1 module, 2 files. The version spread across the layers touches —
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
3 layers, 5 files: the count is the coupling metric.
Put the borrow sequence in order.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
Only the domain decides whether a loan is allowed; presentation and storage just carry the request in and the record out.
What the boundary buys
- Reminder delivery sits behind a
Notifierinterface 接口 that the domain owns. The domain only callssend; it never learns whether the message goes by email or push.
# domain: owns the interface
class Notifier:
def send(self, member, text): ...
# domain: no email or web imports
class LoanService:
def __init__(self, notifier):
self.notifier = notifier
def remind(self, loan):
text = loan.title + " is overdue"
self.notifier.send(loan.member, text)
# outer layer: one way to deliver
class EmailNotifier(Notifier):
def send(self, member, text):
smtp_send(member.email, text)
# the one line that changes for push
service = LoanService(EmailNotifier())
- Swapping to push costs one new class and one changed line, with no domain changes. A test can pass in a fake
Notifierthat just records messages, so the domain is tested with no mail server. - If
LoanServiceimported the email library itself, the dependency would point outward and swapping delivery methods or testing without a mail server would be harder.
Swapping the Notifier strategy costs —
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Behind an interface: the boundary buys the swap.
Swapping email reminders for push notifications behind the Notifier interface means editing the domain layer.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
The domain calls only the interface, so the swap is a new implementation and one configuration line, with zero domain edits. That is the boundary's payoff.
Patterns name the change they absorb
- A design pattern 设计模式 is a named trade-off. Three in the library system:
| Pattern | Here it… | So you can… |
|---|---|---|
| Strategy 策略模式 | puts reminder delivery behind Notifier |
swap email for push |
| Observer 观察者模式 | lets the reminder service listen for "loan overdue" | add listeners without changing the loan |
| Repository 仓储模式 | hides storage behind an interface the domain owns | change the database |
- All three apply dependency inversion 依赖倒置: the high-level policy defines the interface, and the low-level detail implements it. This is the inward dependency rule of the layers, written as code.
Swapping email reminders for push notifications without touching the domain uses which pattern?
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
The delivery method is an interchangeable strategy behind the Notifier interface.
The domain layer should import the web framework to read requests directly.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
That points a dependency outward and makes the domain untestable without the framework.
Instability, counted
- For a module, count incoming dependencies $C_a$ (who uses it) and outgoing dependencies $C_e$ (what it uses). Instability 不稳定度 $I = C_e/(C_a + C_e)$ runs from 0 (everyone depends on it, and it depends on nothing) to 1.
- The domain should be near 0 and the web layer near 1. With $C_a = 2$ and $C_e = 6$, $I = \mathbf{0.75}$: a module that is easy to change, and should be, because little depends on it.
A module with $C_a = 2$ incoming and $C_e = 6$ outgoing dependencies: instability $I$?
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
$6/(2 + 6) = 0.75$.
The domain has $C_a = 6$ et $C_e = 0$: its instability?
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
$0/6 = 0$: maximally stable, as the dependency rule wants.
Worked example: talk only to friends
- This call reaches through three objects:
member.account().wallet().debit(fee)
- A change to
Walletnow breaks the code that borrows books. This breaks the Law of Demeter 迪米特法则: call methods only on yourself, your fields, your parameters and objects you create. - Write
member.payFee(fee)instead, and letMemberpass the work on. The caller now depends on 1 class instead of 3, and the chain can change without affecting it.
order.customer().address().city() follows the Law of Demeter.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
It reaches through three objects; ask order for what you need.
Marks that slip away
- The domain importing the framework. It reverses the dependency rule 依赖规则 and can make the core harder to test independently.
- Arrows without labels. A diagram without labelled dependencies gives reviewers nothing to check.
- A pattern for its own sake. Each pattern absorbs one kind of change; without that change it is only extra code.
Recap
- Presentation, domain, storage: dependencies point inward.
- Cohesion 内聚 keeps one change in one module: $1$ module and $2$ files, not $3$ layers and $5$ files.
- A boundary interface turns a swap into one configuration line.
Que s’est-il passé ? Seule l’équipe du site voit ces informations.