انتقل إلى المحتوى
أمين أبوزيد
JNR-004

مبادئ SOLID

  • 7 درس
  • 72 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • SRP
  • OCP
  • LSP
  • ISP
  • DIP مع أمثلة Java عملية

تعلّم أولًا

72 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. State each SOLID principle and identify a violation in Java code.
  2. Refactor a violation using composition, interfaces, or class extraction.
  3. Explain how dependency inversion enables testing and is the foundation of Spring's DI.
  4. Recognize when applying a principle would add needless complexity.

الدروس

  1. 01المسؤولية الواحدة (Single Responsibility)10 د قراءة
  2. 02المفتوح/المغلق (Open/Closed)11 د قراءة
  3. 03استبدال ليسكوف (Liskov Substitution)10 د قراءة
  4. 04فصل الواجهات (Interface Segregation)10 د قراءة
  5. 05انعكاس الاعتماديات (Dependency Inversion)10 د قراءة
  6. 06تطبيق SOLID بحكمة11 د قراءة
  7. 07عرض عملي: انعكاس الاعتماديات لمزوّد دفع10 د قراءة

المختبر والتمارين

Lab

JNR-004-LAB1 · Pluggable pricing

Outcomes: 1–4 · Time: ~2 h Refactor Shop Core so tax rules and payment providers are injected through interfaces. Add a new tax rule without modifying existing classes. Write a fake payment provider for tests. Acceptance: core has no dependency on concrete providers; a new tax rule is added by adding a class only; tests run without a network.

Exercises (assessment: design quiz with code snippets plus the lab; six exercises)

IDOutcomeTask
JNR-004-EX011State each SOLID letter in one sentence each.
JNR-004-EX021A ReportService queries a database, formats a PDF, and emails it. Which principle does it break?
JNR-004-EX032Refactor if (type.equals("PERCENT")) ... else if (type.equals("FIXED")) ... so a new type needs no edit to existing code.
JNR-004-EX041A ReadOnlyList extends MyList throws UnsupportedOperationException in add. Which principle is broken and how do you fix it?
JNR-004-EX053Make CheckoutService depend on a PaymentProvider interface and test it with a fake.
JNR-004-EX064Give an example where an interface with a single implementation is needless. Say what you would do instead.
أظهر الحلول
  • EX01 S: one reason to change. O: extend without editing. L: subtypes work wherever the parent does. I: small, focused interfaces. D: depend on interfaces, not concrete classes.
  • EX02 SRP. Split into ReportQuery, PdfFormatter, ReportMailer.
  • EX03 Introduce interface Discount { Money apply(Money p); } with a class per type; a new type is a new class (OCP).
  • EX04 LSP. Do not inherit; separate ReadableList and MutableList interfaces (ISP helps too).
  • EX05 CheckoutService(PaymentProvider p); test with class FakePayments implements PaymentProvider { ... } returning a fixed receipt.
  • EX06 A PriceFormatter interface with only one class and no test need. Use the class directly; extract an interface when a second implementation or test double is needed.
  • The assessment design quiz covers outcome 1 and 4 further; outcomes 2–3 are covered by the lab.

الاختبار

Outcomes: O1 state principles and find violations · O2 refactor · O3 dependency inversion and DI · O4 judgment.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1The "S" in SOLID stands for:O1
  2. 2A class that changes for tax, email, and database reasons breaks:O1
  3. 3An override that throws UnsupportedOperationException usually breaks:O1
  4. 4To follow Open/Closed for a new discount type you:O2
  5. 5A huge interface with many unused methods is fixed by:O2
  6. 6Dependency inversion means high-level code depends on:O3
  7. 7Constructor injection helps testing because:O3
  8. 8Spring's dependency injection is built on:O3
  9. 9An interface with one implementation and no need to swap it is:O4
  10. 10When should you apply a principle?O4

الجزء العملي

Six exercises JNR-004-EX01 to EX06 plus the lab, with the design quiz as the written assessment. Pass: at least 8 of 10 quiz answers, five exercises correct, and a core with no dependency on a concrete provider.

أخطاء شائعة

An interface for every class; "SRP" meaning one method per class; LSP violations via throwing UnsupportedOperationException.

التقييم

Design quiz with code snippets plus the lab.

الصلة بالمسار الوظيفي

Design discussions at every level; architecture styles (MID-059).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.