Przegląd zasad SOLID
Poznają Państwo pięć zasad SOLID: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation i Dependency Inversion.
Przegląd zasad SOLID to bezpłatna lekcja Clean Architecture & Design Patterns in Practice na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clean Architecture & Design Patterns in Practice, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clean Architecture & Design Patterns in Practice zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Przegląd zasad SOLID” jest bezpłatna?
Tak — pełny tekst „Przegląd zasad SOLID” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clean Architecture & Design Patterns in Practice, przejdź na CoddyKit PRO. Kurs Clean Architecture & Design Patterns in Practice zawiera 4 lekcji w sumie.
Co nauczysz się w „Przegląd zasad SOLID”?
Poznają Państwo pięć zasad SOLID: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation i Dependency Inversion. Ćwiczysz Clean Architecture & Design Patterns in Practice z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Clean Architecture & Design Patterns in Practice?
Nie wymagamy żadnego doświadczenia. Clean Architecture & Design Patterns in Practice w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Przegląd zasad SOLID”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Clean Architecture & Design Patterns in Practice?
Tak. Każda lekcja Clean Architecture & Design Patterns in Practice zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do Clean Code
- Przegląd zasad SOLID
- Wartość dobrego projektu
- Spójność, sprzężenie i separacja odpowiedzialności