Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( java ) Demo - Strategy Pattern PaymentStategy Interface package StrategyPatternDemoOne; / / Strategy interface interface PaymentStrategy { void pay ( double amount ) ;

(java)Demo - Strategy Pattern
PaymentStategy Interface
package StrategyPatternDemoOne;
// Strategy interface
interface PaymentStrategy {
void pay(double amount);
}
package StrategyPatternDemoOne;
class PayPalStrategy implements PaymentStrategy {
private String email;
private String password;
public PayPalStrategy(String email, String password){
this.email = email;
this.password = password;
}
public void pay(double amount){
System.out.println("Paying "+ amount +" with PayPal.");
// Perform the payment logic using PayPal credentials
}
}
package StrategyPatternDemoOne;
// Concrete strategies
class CreditCardStrategy implements PaymentStrategy {
private String cardNumber;
private String expiryDate;
private String cvv;
public CreditCardStrategy(String cardNumber, String expiryDate, String cvv){
this.cardNumber = cardNumber;
this.expiryDate = expiryDate;
this.cvv = cvv;
}
public void pay(double amount){
System.out.println("Paying "+ amount +" with credit card.");
// Perform the payment logic using credit card details
}
}
package StrategyPatternDemoOne;
// Client code
class ShoppingCart {
private PaymentStrategy paymentStrategy;
public void setPaymentStrategy(PaymentStrategy paymentStrategy){
this.paymentStrategy = paymentStrategy;
}
public void checkout(double amount){
// Perform the checkout process
paymentStrategy.pay(amount);
}
}
Driver:
package StrategyPatternDemoOne;
public class Driver {
// Usage
public static void main(String[] args){
ShoppingCart cart = new ShoppingCart();
// Set the payment strategy dynamically
cart.setPaymentStrategy(new CreditCardStrategy("1234567890123456","12/23","123"));
cart.checkout(100.0);
// Change the payment strategy at runtime
cart.setPaymentStrategy(new PayPalStrategy("example@example.com", "password"));
cart.checkout(50.0);
}
}
Open image-20230711-134214.png
In the example above, the PaymentStrategy interface defines a contract for payment strategies, and CreditCardStrategy and PayPalStrategy are concrete implementations of the payment algorithm. The ShoppingCart class contains a reference to a PaymentStrategy object, which can be set dynamically. The client code can change the payment strategy by calling the setPaymentStrategy() method.
By applying the Strategy pattern, the client code can switch between different payment strategies without modifying the core logic of the ShoppingCart class. This flexibility is especially useful when you have multiple algorithms or behaviors that need to be dynamically selected or replaced based on varying requirements.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions