مبادئ SOLID
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- SRP
- OCP
- LSP
- ISP
- DIP مع أمثلة Java عملية
تعلّم أولًا
72 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- State each SOLID principle and identify a violation in Java code.
- Refactor a violation using composition, interfaces, or class extraction.
- Explain how dependency inversion enables testing and is the foundation of Spring's DI.
- Recognize when applying a principle would add needless complexity.
الدروس
- 01المسؤولية الواحدة (Single Responsibility)10 د قراءة
- 02المفتوح/المغلق (Open/Closed)11 د قراءة
- 03استبدال ليسكوف (Liskov Substitution)10 د قراءة
- 04فصل الواجهات (Interface Segregation)10 د قراءة
- 05انعكاس الاعتماديات (Dependency Inversion)10 د قراءة
- 06تطبيق SOLID بحكمة11 د قراءة
- 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-004-EX01 | 1 | State each SOLID letter in one sentence each. |
| JNR-004-EX02 | 1 | A ReportService queries a database, formats a PDF, and emails it. Which principle does it break? |
| JNR-004-EX03 | 2 | Refactor if (type.equals("PERCENT")) ... else if (type.equals("FIXED")) ... so a new type needs no edit to existing code. |
| JNR-004-EX04 | 1 | A ReadOnlyList extends MyList throws UnsupportedOperationException in add. Which principle is broken and how do you fix it? |
| JNR-004-EX05 | 3 | Make CheckoutService depend on a PaymentProvider interface and test it with a fake. |
| JNR-004-EX06 | 4 | Give 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
ReadableListandMutableListinterfaces (ISP helps too). - EX05
CheckoutService(PaymentProvider p); test withclass FakePayments implements PaymentProvider { ... }returning a fixed receipt. - EX06 A
PriceFormatterinterface 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).