Skip to main content

Practice ยท 1 of 3

Inject the Payment Gateway

Design a checkout flow around a contract: - interface PaymentGateway { String charge(int cents); } inside Solution โ€” returns a receipt id like "ok-1000". - static class CheckoutService with a constructor taking a PaymentGateway and a method static String checkout(PaymentGateway g, int cents) that delegates to the gateway and returns its result. - Implement static class FakeGateway implements PaymentGateway that returns "ok-" + cents โ€” the test double the service will run against. The tests run the service against the fake (never a real payment API): this is dependency inversion exercised by hand.

Difficulty: intermediate

Back to lesson: Practice: Design Lab: Payments & Shapes