Strategy Design Pattern in java

By | July 6, 2020

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, Comparatormethod 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:

Now Let’s move towards the implementation of the Strategy Design Pattern.

Below is the complete source code:

Strategy Design Pattern

PaymentMethod.java

CreditcardPaymentMethod.java

 

PaypalPaymentMethod.java

 

Product.java

 

Shoppingcart.java

 

ClientTest.java

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.

Leave a Reply

Your email address will not be published.