Architecture and design
도입됨
6:03
소프트웨어 엔지니어링
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| layers/ˈleɪəz/ | Distinct levels arranged above, below or within one another in a structure or system. |
| dependency rule/dɪˈpendənsi ruːl/ | 의존 규칙 |
| coupling/ˈkʌplɪŋ/ | 결합도 |
| Cohesion/kəʊˈhiːʒn/ | how closely the parts inside one module belong together |
| 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 —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
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 —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
3 layers, 5 files: the count is the coupling metric.
Put the borrow sequence in order.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
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 —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Behind an interface: the boundary buys the swap.
Swapping email reminders for push notifications behind the Notifier interface means editing the domain layer.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
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?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
The delivery method is an interchangeable strategy behind the Notifier interface.
The domain layer should import the web framework to read requests directly.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
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$?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$6/(2 + 6) = 0.75$.
The domain has $C_a = 6$ 및 $C_e = 0$: its instability?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$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.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
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.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.