0Pricing
Clean Architecture & Design Patterns in Practice · 课时

单一职责与开闭原则精通

深入掌握 SOLID 原则中的前两项,学习识别职责边界,并在不修改现有代码的情况下扩展行为。

单一职责与开闭原则精通 是 CoddyKit 上的免费 Clean Architecture & Design Patterns in Practice 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clean Architecture & Design Patterns in Practice 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Back to the Foundations

You have explored Dependency Inversion and Interface Segregation. This lesson masters the remaining pair:

  • Single Responsibility Principle (SRP)
  • Open-Closed Principle (OCP)

These two drive most everyday refactoring decisions.

SRP Defined Precisely

SRP says a class should have one reason to change. A reason to change maps to a single actor or stakeholder.

If billing rules and report formatting can change independently, they belong in different classes.

Spotting an SRP Violation

This class mixes calculation, persistence, and presentation.

class Employee {
    double calculatePay() { return 0; }
    void save() { /* DB code */ }
    String reportHtml() { return "<html>"; }
}

Refactoring Toward SRP

Split responsibilities so each changes for one reason.

class PayCalculator { double calculate(Employee e) { return 0; } }
class EmployeeRepository { void save(Employee e) {} }
class EmployeeReporter { String html(Employee e) { return "<html>"; } }

The Cohesion Payoff

After the split, each class is more cohesive: everything inside relates to one job.

Changes are localized, tests are focused, and accidental coupling between unrelated concerns disappears.

OCP Defined

The Open-Closed Principle: software entities should be open for extension but closed for modification.

You should be able to add new behavior by writing new code, not editing existing, tested code.

An OCP Violation

Adding a shape forces editing this method every time.

double area(Shape s) {
    if (s.type.equals("circle")) return 3.14 * s.r * s.r;
    else if (s.type.equals("square")) return s.side * s.side;
    return 0;
}

Closing It With Polymorphism

Make each shape compute its own area. New shapes require no edits to existing code.

interface Shape { double area(); }
class Circle implements Shape {
    double r;
    public double area() { return 3.14 * r * r; }
}
class Square implements Shape {
    double side;
    public double area() { return side * side; }
}

OCP Through Strategy and Plugins

Common OCP-enabling techniques:

  • Polymorphism over conditionals.
  • The Strategy pattern to inject varying behavior.
  • Plugin or registry mechanisms for adding handlers.

All let you extend by adding, not editing.

How SRP and OCP Reinforce Each Other

A class with a single responsibility is much easier to keep closed for modification, because there is only one axis of change.

When you cleanly separate responsibilities, extension points emerge naturally.

Pragmatic Limits

Do not over-apply. Premature abstraction for variation that never comes adds needless complexity.

Apply OCP at the points your domain actually varies; let the rest stay simple until change demands it.

Quick Check

Test your grasp of SRP and OCP.

Recap

You mastered the first two SOLID principles.

  • SRP: one reason to change per class.
  • OCP: extend by adding, not editing.
  • They reinforce each other and guide most refactorings, applied where variation truly exists.

常见问题解答

「单一职责与开闭原则精通」课时是免费的吗?

是的 — 「单一职责与开闭原则精通」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clean Architecture & Design Patterns in Practice 课程的其余内容,请升级到 CoddyKit PRO。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

「单一职责与开闭原则精通」这节课中我会学到什么?

深入掌握 SOLID 原则中的前两项,学习识别职责边界,并在不修改现有代码的情况下扩展行为。 你通过在浏览器中直接运行的动手代码来练习 Clean Architecture & Design Patterns in Practice,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clean Architecture & Design Patterns in Practice 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clean Architecture & Design Patterns in Practice 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「单一职责与开闭原则精通」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clean Architecture & Design Patterns in Practice 课中编写并运行代码吗?

能。每节 Clean Architecture & Design Patterns in Practice 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 深入理解依赖倒置
  2. 实践接口隔离
  3. 使用设计模式重构
  4. 单一职责与开闭原则精通
← 返回 Clean Architecture & Design Patterns in Practice