HomeChallengesRepsProblemsLeaderboardTake a challenge

Delete the switch

Three methods switch on the same channel code. Product wants Mobile Money next, which today means editing all three. Replace the code with a class per channel, then add Mobile Money as one new class.

Advanced20 min on the clock5 graded checks · 100 pointsDesigning an Object ModelPro

See alsoOne caller, many shapesThe same switch-to-classes move in JavaScript, graded by running the code.

Graded by reading your code against the checklist below.

What you build

  • Create an abstract PaymentChannel with Fee, DailyLimit and Describe
  • Give each channel its own class that overrides them
  • Make Checkout depend only on PaymentChannel
  • Check the daily limit once, in the base class
  • Add MobileMoneyChannel as a new class: 1%, minimum 10, capped at 1,000; limit 300,000

Done means

No switch on a channel code, a class per channel overriding an abstract base, Checkout using only the base, and Mobile Money added as one class.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else.

  1. No switch on the channel code+25New features without editing old code
  2. Every channel overrides an abstract base+20Let the object answer
  3. Checkout knows only PaymentChannel+15Keeping the promises you inherit
  4. Mobile Money is one new class+20New features without editing old code
  5. The limit is checked once, in the base+20New features without editing old code

What it teaches

Keeping the promises you inheritadvanced

Anywhere the parent works the child must work too — and a subclass that compiles perfectly can still break the first caller who relied on something the parent quietly promised.

The rest of Designing an Object Model