انتقل إلى المحتوى
أمين أبوزيد
قيد المراجعةJNR-005-L10

عرض عملي: استبدال switch على النوع بنمط Strategy

  • الدرس 10 من 10
  • 11 دقيقة قراءة
  • Junior
في هذا الدرس 3 موضوعات

محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.

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

في هذا الدرس

turn a growing switch on shipping type into Strategy. Prepares JNR-005-LAB1.

تفاصيل المخطط المبدئي

خطوة بخطوة

  1. Show a switch (shippingType) in ShippingCalculator with three cases; tests pass.
  2. Extract the ShippingStrategy interface and one class per case.
  3. Choose the strategy through a small factory.
  4. Planned mistake: create a strategy for a case with one line of code and a new file for each trivial detail; the design is heavier than the switch. Fix: use lambdas for tiny strategies, and keep only the meaningful classes.
  5. Run the tests; add a fourth shipping type with no edit to old classes.
  6. Say in one line why Strategy was justified here.

اختبر فهمك

When is a lambda enough for a strategy?

أظهر الإجابة

When the behavior is tiny and has no state.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

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

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

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