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:
Step 1: Identify fixed workflow
The payment process has a fixed sequence: validate, authenticate, execute transaction, notify user.
Step 2: Use Template Method pattern
Define an abstract base class PaymentProcessor with a final processPayment() method implementing the fixed sequence.
Step 3: Allow customization
Make executeTransaction() abstract so subclasses implement payment-specific logic.
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.