Design patterns: naming a solution that keeps recurring · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
به انگلیسی

Names for solutions that keep recurring

Computing · Software Engineering · ages 19-21
Name ______________________   Date ____________
  1. Within one running process, a class controls creation so callers share its one instance. Which arrangement does this describe?

    • Singleton
    • Observer
    • Strategy
  2. What does a design pattern's name give a team?

    • A rule requiring the same classes in every project
    • A shared term for a recurring design and its context
    • A guarantee that using it always makes programs faster
  3. Once a design has a recognised pattern name, it needs no checks against its requirements.

    Circle one:   True   False

  4. A sensor calls each registered listener's update(value) after a change. A second board is added. Which change keeps the sensor independent of concrete board classes?

    • Register the new board as an update(value) listener.
    • Make the sensor identify the new board class and update it directly.
    • Put the new board's display code inside the sensor's change handler.
  5. Python runs these statements in order. Both objects honour quote(total)'s contract. Which pattern does this trace illustrate?

    pricing = weekday_pricer

    pricing.quote(total)

    pricing = weekend_pricer

    pricing.quote(total)

    • Singleton
    • Observer
    • Strategy
  6. An account delegates sending to a sender and storage to a store. Retry logic changes, but the sender keeps the whole send(message) contract. Where should that implementation change go?

    • In the account, by copying retry logic into every call
    • Inside the sender, behind the unchanged send(message) contract
    • In the store, by making save(message) handle all the sender's retry decisions
  7. One direct function meets all requirements for a fixed calculation. No replacement or separate testing is needed. A teammate proposes three swapping layers with no other benefit. Which review comment fits?

    • Keep the direct function; the stated requirements do not justify adding swapping boundaries.
    • Keep one swapping layer to test the fixed calculation independently of its caller.
    • Keep all three layers to prepare for later replacement rules, avoiding caller changes if the calculation varies.
  8. Python runs these statements in order:

    LIMIT = 24

    cached_limit = LIMIT

    LIMIT = 36

    The next check reads cached_limit. Which assignment, inserted at the stated point, makes that check use 36?

    • Immediately after LIMIT = 36, insert LIMIT = cached_limit.
    • Immediately before LIMIT = 36, insert cached_limit = LIMIT.
    • Immediately after LIMIT = 36, insert cached_limit = LIMIT.
LightMySky · lightmysky.comW1-mt_bhUEk8PwBp-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Names for solutions that keep recurring W1-mt_bhUEk8PwBp-s1

  1. Singleton · Controlled creation and access to one shared instance describe Singleton. The stated scope here is one running process.
  2. A shared term for a recurring design and its context · The name refers to a recurring arrangement and the context where it helps. It supplies shared vocabulary, not identical implementations or a speed guarantee.
  3. False · A pattern name identifies an arrangement. The implementation still needs checking against the requirements and the promises between its parts.
  4. Register the new board as an update(value) listener. · The new board can register as a listener and provide update(value). The sensor keeps using its notification contract without adding board-specific code.
  5. Strategy · The calls use weekday_pricer and then weekend_pricer through the same quote(total) contract. Selecting the collaborator that supplies the calculation illustrates Strategy.
  6. Inside the sender, behind the unchanged send(message) contract · Retry logic belongs to the sender's implementation. Preserving the whole send(message) contract lets the account keep its existing call.
  7. Keep the direct function; the stated requirements do not justify adding swapping boundaries. · The direct function meets every stated requirement. Separate testing is not needed, and future replacement is not a stated requirement. Neither argument justifies the proposed swapping layers for this case.
  8. Immediately after LIMIT = 36, insert cached_limit = LIMIT. · Assigning cached_limit = LIMIT after LIMIT becomes 36 copies 36 into cached_limit. Doing it before copies 24. Reversing the assignment afterwards changes LIMIT to 24 without refreshing cached_limit.
Worksheet · LightMySky