Clean Architecture & Design Patterns in Practice · درس

نظرة عامة على مبادئ SOLID

تعرّفوا بصورة تمهيدية على مبادئ SOLID الخمسة: المسؤولية الواحدة، والانفتاح/الانغلاق، واستبدال Liskov، وفصل الواجهات، وعكس الاعتماديات.

الدرس 2 من 412 خطوة

نظرة عامة على مبادئ SOLID درس مجاني في Clean Architecture & Design Patterns in Practice على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.

البدء مجانًا

تعلم Clean Architecture & Design Patterns in Practice مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «نظرة عامة على مبادئ SOLID» مجاني؟

نعم — نص درس «نظرة عامة على مبادئ SOLID» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clean Architecture & Design Patterns in Practice، انتقل إلى CoddyKit PRO. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.

ماذا ستتعلم في «نظرة عامة على مبادئ SOLID»؟

تعرّفوا بصورة تمهيدية على مبادئ SOLID الخمسة: المسؤولية الواحدة، والانفتاح/الانغلاق، واستبدال Liskov، وفصل الواجهات، وعكس الاعتماديات. تتمرن على Clean Architecture & Design Patterns in Practice مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Clean Architecture & Design Patterns in Practice؟

لا تُشترط خبرة سابقة. Clean Architecture & Design Patterns in Practice على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «نظرة عامة على مبادئ SOLID»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Clean Architecture & Design Patterns in Practice هذا؟

نعم. كل درس في Clean Architecture & Design Patterns in Practice يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى التعليمات البرمجية النظيفة
  2. نظرة عامة على مبادئ SOLID
  3. قيمة التصميم الجيد
  4. التماسك والاقتران وفصل الاهتمامات
← العودة إلى Clean Architecture & Design Patterns in Practice