Factory
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
hide how objects are created.
الموضوعات المخطط لها
- Problem: callers should not know which class to create.
- A factory method returns the right object.
- Use it when creation has rules or choices.
- Return an interface or sealed type, not the concrete class.
- JDK examples:
List.of,Optional.of,Calendar.getInstance.
تفاصيل المخطط المبدئي
عرض عملي
static Discount discountFor(String code) {
return switch (code) {
case "SUMMER10" -> new Percentage(10);
case "WELCOME5" -> new FixedAmount(Money.eur(500));
default -> throw new IllegalArgumentException(code);
};
}اختبر فهمك
What does a factory hide?
أظهر الإجابة
Which concrete class is created and how.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.