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

مقدمة في أنماط التصميم

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

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

نظرة عامة

الموضوعات

  • Factory
  • Builder
  • Strategy
  • Adapter
  • Decorator
  • Observer
  • Repository

تعلّم أولًا

108 دقيقة قراءة · 4 ساعة تدريب (تقدير)

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

  1. Implement Factory, Builder, Strategy, Adapter, Decorator, Observer, and Repository in Java.
  2. Describe the problem each pattern solves and choose one for a given scenario.
  3. Recognize these patterns in the JDK and Spring.
  4. Avoid applying a pattern where a simple function or class suffices.

الدروس

  1. 01ما هي الأنماط (وما ليست عليه)11 د قراءة
  2. 02Factory10 د قراءة
  3. 03Builder11 د قراءة
  4. 04Strategy10 د قراءة
  5. 05Adapter11 د قراءة
  6. 06Decorator11 د قراءة
  7. 07Observer11 د قراءة
  8. 08Repository11 د قراءة
  9. 09اكتشاف الأنماط في Spring11 د قراءة
  10. 10عرض عملي: استبدال switch على النوع بنمط Strategy11 د قراءة

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

Lab

JNR-005-LAB1 · Patterns in Shop Core

Outcomes: 1–4 · Time: ~2.5 h Add four patterns to Shop Core: Strategy for shipping, Adapter for a third-party currency API, Decorator for logging or caching a price lookup, Observer for "order placed" notifications. Acceptance: each pattern is justified in the README (problem, why this pattern, one alternative considered); tests for each; no pattern where a simple function would do.

Exercises (assessment: scenario quiz plus the lab; eight exercises)

IDOutcomeTask
JNR-005-EX011Write a DiscountFactory.of(String code) that returns a Discount; unknown code throws.
JNR-005-EX021Write a builder for Order with customer, lines, and an optional coupon; build() rejects an empty order.
JNR-005-EX031Implement ShippingStrategy as two classes and one lambda, and pick one by name.
JNR-005-EX041Write an adapter from VendorApi.rate(String, String) to your CurrencyRates interface.
JNR-005-EX051Write a LoggingPriceLookup decorator, and a ProductRepository in-memory implementation.
JNR-005-EX061Implement an OrderPlaced listener list with two listeners.
JNR-005-EX072Which pattern solves each: many constructor parameters; swap tax algorithm; wrap a legacy API; add caching; notify on events; hide SQL.
JNR-005-EX083, 4Name one JDK and one Spring example for three patterns. Then describe a case where a pattern is needless.
أظهر الحلول
  • EX01 switch (code) { case "SUMMER10" -> new Percentage(10); ... default -> throw new IllegalArgumentException(code); }.
  • EX02 Static nested Builder with fluent setters; build() checks lines.isEmpty() and returns an immutable Order.
  • EX03 ShippingStrategy standard = o -> Money.eur(499); and a pick via Map<String, ShippingStrategy>.
  • EX04 class VendorRatesAdapter implements CurrencyRates { public Money convert(...) { double r = api.rate(...); ... } }.
  • EX05 The decorator holds a PriceLookup inner, logs, then calls inner.priceOf; the repository wraps a Map<Sku, Product>.
  • EX06 List<OrderListener>; loop and call onPlaced. Do not depend on order.
  • EX07 Builder; Strategy; Adapter; Decorator; Observer; Repository.
  • EX08 Examples: Factory List.of / Spring BeanFactory; Strategy Comparator / JdbcTemplate callbacks; Decorator BufferedReader / Spring proxies. Needless: Builder for a two-field object.
  • Outcome 2 and 4 are also covered by the scenario quiz in the assessment and quiz Q07–Q10.

الاختبار

Outcomes: O1 implement the patterns · O2 problem and choice · O3 patterns in JDK and Spring · O4 avoid needless patterns.

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

  1. 1A factory method:O1
  2. 2A Decorator implements:O1
  3. 3Which pattern removes an if/else that chooses an algorithm?O2
  4. 4Which pattern fits a third-party API with a different shape?O2
  5. 5Many optional constructor parameters suggest:O2
  6. 6"Notify email and stock when an order is placed" suggests:O2
  7. 7Comparator passed to sort is an example of:O3
  8. 8BufferedReader wrapping a Reader is an example of:O3
  9. 9A Builder for an object with two fields is:O4
  10. 10Which statement is right?O4

الجزء العملي

Eight exercises JNR-005-EX01 to EX08 plus the lab, with the scenario quiz (which pattern and why) as the written assessment. Pass: at least 8 of 10 quiz answers, seven exercises correct, and a README that justifies each lab pattern.

أخطاء شائعة

Pattern-driven design; Builder for two-field objects; Observer with hidden ordering assumptions.

التقييم

Scenario quiz (which pattern, and why) plus the lab.

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

Shared vocabulary for design reviews; Observer grows into event-driven design (MID-026).

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