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

SOLID 原则概览

初步了解 SOLID 的五项原则:单一职责、开闭原则、里氏替换、接口隔离和依赖倒置

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

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

What are SOLID Principles?

The SOLID principles are five design guidelines for software that’s easy to maintain, understand, and extend. Think of them as a path to cleaner code.

Why SOLID Matters

Applying SOLID gives you code that’s easier to understand, simpler to test, more flexible to change, and friendlier to teamwork. It’s the bedrock of good OO design.

S: Single Responsibility Principle (SRP)

The Single Responsibility Principle: a class should have one reason to change. Don’t mix calculating data and saving it — split those concerns apart.

SRP in Action: Simple Report

This SimpleReport obeys SRP: its only job is generating report content. Printing and saving are someone else’s responsibility.

public class SimpleReport {
  private String content;

  public SimpleReport(String content) {
    this.content = content;
  }

  // This class's single responsibility is to generate/represent the report content
  public String generateReportContent() {
    return "Report: " + content;
  }

  public static void main(String[] args) {
    SimpleReport report = new SimpleReport("Sales Data for Q1");
    System.out.println(report.generateReportContent());
  }
}

O: Open/Closed Principle (OCP)

The Open/Closed Principle: software should be open for extension but closed for modification. Add new behavior without editing working code.

OCP in Action: Flexible Greeters

Here a new greeter type slots in without touching the Greeter interface or existing classes — extending behavior, not modifying it. That’s OCP.

interface Greeter {
  String greet();
}

class EnglishGreeter implements Greeter {
  @Override
  public String greet() {
    return "Hello!";
  }
}

class SpanishGreeter implements Greeter {
  @Override
  public String greet() {
    return "¡Hola!";
  }
}

public class OCPDemo {
  public static void main(String[] args) {
    Greeter english = new EnglishGreeter();
    Greeter spanish = new SpanishGreeter();
    System.out.println(english.greet());
    System.out.println(spanish.greet());
  }
}

L: Liskov Substitution Principle (LSP)

The Liskov Substitution Principle: a subclass must be usable anywhere its parent is expected, without breaking the program. Subtypes honor the contract.

LSP in Action: Shapes

Any function taking a Shape handles both Rectangle and Circle correctly — they honor the contract. That’s LSP at work.

interface Shape {
  double getArea();
}

class Rectangle implements Shape {
  private double width; 
  private double height;

  public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
  }

  @Override
  public double getArea() {
    return width * height;
  }
}

class Circle implements Shape {
  private double radius;

  public Circle(double radius) {
    this.radius = radius;
  }

  @Override
  public double getArea() {
    return Math.PI * radius * radius;
  }
}

public class LSPDemo {
  public static void printArea(Shape shape) {
    System.out.println("Area: " + shape.getArea());
  }

  public static void main(String[] args) {
    Shape myRectangle = new Rectangle(5, 4);
    Shape myCircle = new Circle(3);

    printArea(myRectangle);
    printArea(myCircle);
  }
}

I: Interface Segregation Principle (ISP)

The Interface Segregation Principle: don’t force clients to depend on methods they don’t use. Prefer many small, focused interfaces over one fat one.

D: Dependency Inversion Principle (DIP)

The Dependency Inversion Principle: high-level and low-level modules should both depend on abstractions, not concrete details. That’s how you get loose coupling.

Check Your Understanding

Which of the following statements correctly describe the benefits of applying SOLID principles?

Recap: The Power of SOLID

You’ve met all five SOLID principles — single responsibility, open/closed, Liskov, interface segregation, dependency inversion — the toolkit for adaptable software.

常见问题解答

「SOLID 原则概览」课时是免费的吗?

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

「SOLID 原则概览」这节课中我会学到什么?

初步了解 SOLID 的五项原则:单一职责、开闭原则、里氏替换、接口隔离和依赖倒置 你通过在浏览器中直接运行的动手代码来练习 Clean Architecture & Design Patterns in Practice,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「SOLID 原则概览」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 整洁代码简介
  2. SOLID 原则概览
  3. 良好设计的价值
  4. 内聚、耦合与关注点分离
← 返回 Clean Architecture & Design Patterns in Practice