انعكاس الاعتماديات (Dependency Inversion)
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
make high-level code depend on interfaces, not on concrete details.
الموضوعات المخطط لها
- D: high-level code and low-level code both depend on an interface.
- Like the socket: the kitchen depends on the socket shape, not the appliance.
- Pass dependencies in through the constructor (dependency injection).
- Benefit: in tests, pass a fake instead of the real thing.
- Spring's dependency injection is built on this idea.
تفاصيل المخطط المبدئي
عرض عملي
interface PaymentProvider { Receipt charge(Money amount); }
class CheckoutService {
private final PaymentProvider payments;
CheckoutService(PaymentProvider payments) { this.payments = payments; }
}اختبر فهمك
Why does DI make testing easier?
أظهر الإجابة
You can pass a fake in place of the real dependency.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.