In this post, We will talk and learn about the Strategy Design Pattern in Java.
Key Points About Strategy Design Pattern :
- Strategy design pattern falls under behavioral design pattern.
- This Pattern is really useful when we have multiple algorithms present for any specific tasks and the client decides the actual implementation to be used at runtime.
- We can define multiple algorithms and let client applications pass the algorithm to be used as a parameter. This pattern is really very useful when we have present multiple algorithms for any specific task and we want our application to be flexible to choose any of the algorithms at runtime for a specific task
- One of the good examples of the strategy pattern is the Collections.sort(list, Comparator) method that takes the Comparator parameter, and based on the different implementations of the Comparator interface the Objects are getting sorted in different ways.
Strategy Design Pattern in JDK:
- Collections.sort(list) & Collections.sort(list,Comparator) method
- javax.servlet.http.HttpServlet: service() method, plus all of the doXXX() methods that accept HttpServletRequestand HttpServletResponse objects as arguments.
Now Let’s move towards the implementation of the Strategy Design Pattern.
Below is the complete source code:
PaymentMethod.java
1 2 3 4 5 6 7 8 9 |
package com.kkjavatutorials.payment; /** * @author KK JavaTutorials * Contract to implelemt amont to be paid */ public interface PaymentMethod { public abstract void pay(int amount); } |
CreditcardPaymentMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.kkjavatutorials.payment.impl; import com.kkjavatutorials.payment.PaymentMethod; /** * @author KK JavaTutorials *This class supports Creditcard Payment Method */ public class CreditcardPaymentMethod implements PaymentMethod { private String cardHolderName; private String card; private String cvv; private String dateOfExpiry; public CreditcardPaymentMethod(String cardHolderName, String card, String cvv, String dateOfExpiry) { super(); this.cardHolderName = cardHolderName; this.card = card; this.cvv = cvv; this.dateOfExpiry = dateOfExpiry; } public String getCardHolderName() { return cardHolderName; } public String getCard() { return card; } public String getCvv() { return cvv; } public String getDateOfExpiry() { return dateOfExpiry; } @Override public void pay(int amount) { System.out.println(amount +" is paid using debit card :"+card); } } |
PaypalPaymentMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kkjavatutorials.payment.impl; import com.kkjavatutorials.payment.PaymentMethod; /** * @author KK JavaTutorials * This class supports Paypal Payment Method */ public class PaypalPaymentMethod implements PaymentMethod { private String email; private String password; public PaypalPaymentMethod(String email, String password) { super(); this.email = email; this.password = password; } public String getEmail() { return email; } public String getPassword() { return password; } @Override public void pay(int amount) { System.out.println(amount+" is paid using Paypal"); } } |
Product.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkjavatutorials.product; /** * @author KK JavaTutorials *This Model class represent product information */ public class Product { private String name; private String upcCode; private int price; public Product(String name, String upcCode, int price) { super(); this.name = name; this.upcCode = upcCode; this.price = price; } public String getName() { return name; } public String getUpcCode() { return upcCode; } public int getPrice() { return price; } } |
Shoppingcart.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.kkjavatutorials.shoppingcart; import java.util.ArrayList; import java.util.List; import com.kkjavatutorials.payment.PaymentMethod; import com.kkjavatutorials.product.Product; /** * @author KK JavaTutorials * This class provides methods to: * addProduct -- > add product in shopping cart * removeProduct -- > remove product from shopping cart * calculateTotalPrice --> calculate total price of the products added in cart * payment -- > proceed for payment(This is where Strategy design pattern * is getting used,this method accept various algorithms for payment like * PaypalPayment & CreditcardPayment etc..) */ public class Shoppingcart { private List<Product> productList; public Shoppingcart() { productList = new ArrayList<Product>(); } public void addProduct(Product product) { productList.add(product); } public void removeProduct(Product product) { productList.remove(product); } private int calculateTotalPrice() { return productList.stream().map(p->p.getPrice()).reduce(0, Integer::sum); } public void payment(PaymentMethod paymentMethod) { paymentMethod.pay(calculateTotalPrice()); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.kkjavatutorials.client; import com.kkjavatutorials.payment.impl.CreditcardPaymentMethod; import com.kkjavatutorials.payment.impl.PaypalPaymentMethod; import com.kkjavatutorials.product.Product; import com.kkjavatutorials.shoppingcart.Shoppingcart; /** * @author KK JavaTutorials * Client program is able to proceed with payment * Strategies like CreditcardPayment or PaypalPayment etc.. */ public class ClientTest { public static void main(String[] args) { //Creating first Instance of Shoppingcart Shoppingcart shoppingcart = new Shoppingcart(); Product product1 = new Product("Soap", "88889895", 100); Product product2 = new Product("Shampoo", "89989895", 120); Product product3 = new Product("Cookies", "84449895", 100); //adding three product in shopping cart shoppingcart.addProduct(product1); shoppingcart.addProduct(product2); shoppingcart.addProduct(product3); //Proceed to payment Strategy as CreditcardPayment shoppingcart.payment(new CreditcardPaymentMethod("KK", "987654326372626", "898", "11/23")); System.out.println("--------------------------------------------------------"); //Creating Second Instance of Shoppingcart shoppingcart = new Shoppingcart(); product1 = new Product("Milk", "888009895", 200); product2 = new Product("Beer", "89909995", 320); //adding two products in shopping cart shoppingcart.addProduct(product1); shoppingcart.addProduct(product2); //Proceed to payment Strategy as PaypalPayment } } |
Output of this program:
320 is paid using debit card :987654326372626
——————————————————–
520 is paid using Paypal
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
Mediator Design Pattern in Java
That’s all about the Strategy Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.