Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the Payment Strategy Pattern?
It is a design pattern that lets you choose different payment methods at runtime without changing the code that uses them. It helps keep payment logic separate and flexible.
Click to reveal answer
beginner
Why use the Payment Strategy Pattern in system design?
It allows adding new payment methods easily, supports multiple payment options, and keeps the code clean by separating payment logic from business logic.
Click to reveal answer
intermediate
Name the main components of the Payment Strategy Pattern.
1. Strategy Interface: defines payment method behavior. 2. Concrete Strategies: implement specific payment methods (e.g., credit card, PayPal). 3. Context: uses a strategy to perform payment.
Click to reveal answer
intermediate
How does the Context class interact with payment strategies?
The Context holds a reference to a payment strategy and calls its payment method. It can switch strategies at runtime to support different payment options.
Click to reveal answer
beginner
Give a real-life example of the Payment Strategy Pattern.
An online store lets customers pay by credit card, PayPal, or gift card. Each payment method is a strategy. The store’s checkout system (context) uses the chosen strategy to process payment.
Click to reveal answer
What does the Payment Strategy Pattern help you do?
AStore payment data in a database
BSwitch payment methods without changing client code
CEncrypt payment information
DGenerate invoices automatically
✗ Incorrect
The pattern allows switching payment methods easily without modifying the client code.
Which component defines the payment method interface in the Payment Strategy Pattern?
AContext
BConcrete Strategy
CStrategy Interface
DPayment Gateway
✗ Incorrect
The Strategy Interface defines the methods that all payment strategies must implement.
In the Payment Strategy Pattern, where is the payment method chosen?
AIn the Context class at runtime
BInside the Concrete Strategy
CHardcoded in the client code
DIn the database schema
✗ Incorrect
The Context class holds and uses the chosen payment strategy at runtime.
Which of these is NOT a benefit of using the Payment Strategy Pattern?
AEasier to add new payment methods
BImproves code maintainability
CSupports multiple payment options
DAutomatically detects fraud
✗ Incorrect
Fraud detection is not handled by the Payment Strategy Pattern.
What role does the Concrete Strategy play?
AImplements a specific payment method
BManages user sessions
CDefines the payment interface
DHandles database connections
✗ Incorrect
Concrete Strategies implement specific payment methods like credit card or PayPal.
Explain how the Payment Strategy Pattern improves flexibility in a payment system.
Think about how changing payment methods affects code.
You got /4 concepts.
Describe the roles of Context, Strategy Interface, and Concrete Strategies in the Payment Strategy Pattern.
Focus on how these parts work together.
You got /3 concepts.
Practice
(1/5)
1. What is the main benefit of using the Payment Strategy Pattern in a payment system?
easy
A. It allows switching between different payment methods without changing the main code.
B. It forces all payment methods to use the same currency.
C. It stores all payment data in a single database table.
D. It encrypts payment information automatically.
Solution
Step 1: Understand the purpose of the Payment Strategy Pattern
The pattern is designed to let the system switch payment methods easily without modifying the main logic.
Step 2: Analyze the options
Only It allows switching between different payment methods without changing the main code. describes this benefit correctly. Other options describe unrelated features.
Final Answer:
It allows switching between different payment methods without changing the main code. -> Option A
Hint: Look for 'interface' keyword and method signature format [OK]
Common Mistakes:
Using class instead of interface for strategy definition
Confusing function syntax with interface
Missing method parameter types
3. Given the following code snippet implementing the Payment Strategy Pattern, what will be the output?
class PaymentStrategy {
pay(amount) { throw 'Not implemented'; }
}
class CreditCardPayment extends PaymentStrategy {
pay(amount) { return `Paid ${amount} with Credit Card`; }
}
class PayPalPayment extends PaymentStrategy {
pay(amount) { return `Paid ${amount} with PayPal`; }
}
class PaymentContext {
constructor(strategy) { this.strategy = strategy; }
executePayment(amount) { return this.strategy.pay(amount); }
}
const context = new PaymentContext(new PayPalPayment());
console.log(context.executePayment(100));
medium
A. Paid 100 with Credit Card
B. Not implemented
C. Paid 100 with PayPal
D. Error: strategy.pay is not a function
Solution
Step 1: Trace the object creation and method calls
The PaymentContext is created with a PayPalPayment strategy. Calling executePayment(100) calls PayPalPayment's pay method.
Step 2: Understand the pay method output
PayPalPayment's pay returns 'Paid 100 with PayPal'. This string is printed.
Final Answer:
Paid 100 with PayPal -> Option C
Quick Check:
Context uses PayPalPayment = Output with PayPal [OK]
Hint: Check which strategy instance is passed to context [OK]
Common Mistakes:
Assuming default or CreditCardPayment is used
Expecting an error from base class
Confusing method override behavior
4. Identify the error in the following Payment Strategy Pattern implementation:
class PaymentStrategy {
pay(amount) { console.log('Paying ' + amount); }
}
class BitcoinPayment extends PaymentStrategy {
pay() { console.log('Paying with Bitcoin'); }
}
const payment = new BitcoinPayment();
payment.pay(50);
medium
A. PaymentStrategy should not have a pay method implementation.
B. BitcoinPayment's pay method does not accept the amount parameter.
C. BitcoinPayment should not extend PaymentStrategy.
D. Calling pay with 50 causes a syntax error.
Solution
Step 1: Compare method signatures in base and subclass
PaymentStrategy's pay expects an amount parameter, but BitcoinPayment's pay method does not accept any parameters.
Step 2: Understand the impact of signature mismatch
Calling payment.pay(50) passes an argument, but BitcoinPayment's pay ignores it, causing unexpected behavior or errors.
Final Answer:
BitcoinPayment's pay method does not accept the amount parameter. -> Option B
Quick Check:
Method signature mismatch = BitcoinPayment's pay method does not accept the amount parameter. [OK]
Hint: Check if subclass methods match base method parameters [OK]
Common Mistakes:
Thinking base class should not implement pay
Assuming inheritance is wrong
Confusing runtime error with syntax error
5. You are designing a payment system that must support credit cards, PayPal, and a new cryptocurrency payment method. Using the Payment Strategy Pattern, which design approach best supports adding the new method with minimal changes?
hard
A. Use a global variable to switch payment methods inside the main payment function.
B. Modify the existing CreditCardPayment class to handle cryptocurrency payments.
C. Add cryptocurrency payment logic inside the PaymentContext class directly.
D. Create a new class implementing the PaymentStrategy interface for cryptocurrency and pass it to the payment context.
Solution
Step 1: Understand the open/closed principle in design
The system should be open for extension but closed for modification. Adding a new payment method should not require changing existing classes.
Step 2: Evaluate each option
Create a new class implementing the PaymentStrategy interface for cryptocurrency and pass it to the payment context. creates a new class implementing the interface, fitting the pattern and minimizing changes. Options B and C modify existing classes, violating the principle. Use a global variable to switch payment methods inside the main payment function. uses a global variable, which is poor design.
Final Answer:
Create a new class implementing the PaymentStrategy interface for cryptocurrency and pass it to the payment context. -> Option D
Quick Check:
New class for new method = Create a new class implementing the PaymentStrategy interface for cryptocurrency and pass it to the payment context. [OK]
Hint: Add new payment as new class, avoid changing existing code [OK]