SOLID 原則の概要
SOLID の5原則、Single Responsibility、Open/Closed、Liskov Substitution、Interface Segregation、Dependency Inversion を入門的に学びます。
「SOLID 原則の概要」はCoddyKit上の無料Clean Architecture & Design Patterns in Practiceレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
AI チューターと学ぶ Clean Architecture & Design Patterns in Practice — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「SOLID 原則の概要」レッスンは無料ですか?
はい。「SOLID 原則の概要」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clean Architecture & Design Patterns in Practiceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clean Architecture & Design Patterns in Practiceコースには全4レッスンが含まれています。
「SOLID 原則の概要」で何を学びますか?
SOLID の5原則、Single Responsibility、Open/Closed、Liskov Substitution、Interface Segregation、Dependency Inversion を入門的に学びます。 ブラウザで直接実行するハンズオンコードでClean Architecture & Design Patterns in Practiceを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Clean Code 入門
- SOLID 原則の概要
- 優れた設計の価値
- 凝集度、結合度、関心の分離