Bird
Raised Fist0

Using the Template Method pattern, which design approach best ensures a fixed payment workflow while allowing method-specific processing?

hard📝 Trade-off Q8 of Q15
LLD - Behavioral Design Patterns — Part 1
You are tasked with designing a payment processing system that supports multiple payment methods (Credit Card, PayPal, Cryptocurrency). Using the Template Method pattern, which design approach best ensures a fixed payment workflow while allowing method-specific processing?
ACreate an abstract PaymentProcessor class with a final <code>processPayment()</code> method defining steps: validate(), authenticate(), executeTransaction(), and notifyUser(); subclasses override executeTransaction()
BImplement a PaymentProcessor interface with all methods abstract; each payment method implements all steps independently
CUse a single PaymentProcessor class with conditional logic inside <code>processPayment()</code> to handle all payment types
DDefine separate classes for each payment method without any common base class or shared workflow
Step-by-Step Solution
Solution:
  1. Step 1: Identify fixed workflow

    The payment process has a fixed sequence: validate, authenticate, execute transaction, notify user.
  2. Step 2: Use Template Method pattern

    Define an abstract base class PaymentProcessor with a final processPayment() method implementing the fixed sequence.
  3. Step 3: Allow customization

    Make executeTransaction() abstract so subclasses implement payment-specific logic.
  4. Step 4: Evaluate options

    Create an abstract PaymentProcessor class with a final processPayment() method defining steps: validate(), authenticate(), executeTransaction(), and notifyUser(); subclasses override executeTransaction() matches this design. Options B, C, and D either lack fixed workflow or reuse.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Template method fixes steps; subclasses customize key step [OK]
Quick Trick: Abstract class fixes steps; subclasses override variable parts [OK]
Common Mistakes:
MISTAKES
  • Using interfaces without fixed workflow enforcement
  • Putting all logic in one class with conditionals
  • Not enforcing step order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes