Gambaran Umum Prinsip SOLID
Dapatkan pengenalan terhadap lima prinsip SOLID: Tanggung Jawab Tunggal, Terbuka/Tertutup, Substitusi Liskov, Segregasi Antarmuka, dan Inversi Dependensi.
Gambaran Umum Prinsip SOLID adalah pelajaran Clean Architecture & Design Patterns in Practice gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Clean Architecture & Design Patterns in Practice, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Clean Architecture & Design Patterns in Practice mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Belajar Clean Architecture & Design Patterns in Practice dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Gambaran Umum Prinsip SOLID” gratis?
Ya — teks lengkap “Gambaran Umum Prinsip SOLID” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Clean Architecture & Design Patterns in Practice, upgrade ke CoddyKit PRO. Kursus Clean Architecture & Design Patterns in Practice mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Gambaran Umum Prinsip SOLID”?
Dapatkan pengenalan terhadap lima prinsip SOLID: Tanggung Jawab Tunggal, Terbuka/Tertutup, Substitusi Liskov, Segregasi Antarmuka, dan Inversi Dependensi. Kamu berlatih Clean Architecture & Design Patterns in Practice dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Clean Architecture & Design Patterns in Practice?
Tidak diperlukan pengalaman sebelumnya. Clean Architecture & Design Patterns in Practice di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Gambaran Umum Prinsip SOLID” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Clean Architecture & Design Patterns in Practice ini?
Ya. Setiap pelajaran Clean Architecture & Design Patterns in Practice menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengantar Kode Bersih
- Gambaran Umum Prinsip SOLID
- Nilai Desain yang Baik
- Kohesi, Kopling, dan Pemisahan Tanggung Jawab