Thursday, 27 July 2023

Strategy Design Pattern

       In this post, we need to learn Strategy Design pattern with real time examples. For all design pattern posts refer - Design Patterns
      
      Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithms for a specific task and client decides the actual implementation to be used at runtime. Strategy pattern provides more flexibility, extensibility, and choice.

     There are many real time examples like payment implementation for different payment types,  different sorting algorithms and many more.

     I will take a GST tax example and will discuss in more detail with code, so GST tax varies between each products. There are four slabs - 5% GST, 12%GST, 18% GST and 28% GST(currently it's four slabs, it might change in future) and some of products doesn't have GST I mean it's a zero slab, but for code implementation we are not considering that. 

      Instead of creating and writing logic in four different classes for each slab, we need to follow the Strategy design pattern it's more flexible, readable and reusable code and also it follows the  OOP's design principle.

First step is to create a interface and then for each slab need to create seperate class and implements the interface.

TaxCalculation.java

public interface TaxCalculation {
     public Double calculateTax(Double productPrice);
}

Implemented classes, the overriden method calculateTax method logic will differ for each slab.

FivePercentageTax.java

public class FivePercentageTax implements TaxCalculation{

	@Override
	public Double calculateTax(Double productPrice) {
		return (productPrice * 5) / 100;
	}

}

TwelvePercentageTax.java

public class TwelvePercentageTax implements TaxCalculation{

	@Override
	public Double calculateTax(Double productPrice) {
		return (productPrice * 12) / 100;
	}

}

EighteenPercentageTax.java

public class EighteenPercentageTax implements TaxCalculation {

	@Override
	public Double calculateTax(Double productPrice) {
		return (productPrice * 18) / 100;
	}

}

TwentyEightPercentageTax.java

public class TwentyEightPercentageTax implements TaxCalculation {

	@Override
	public Double calculateTax(Double productPrice) {
		return (productPrice * 28) / 100;
	}

}

The below class - GSTTaxCalculator is the main class where I created a method calculateGSTTax, in this need to pass the TaxCalculation implemented class object and using object we can call the claculateTax method it should invoke corresponding tax implementation method.

GSTTaxCalculator.java

public class GSTTaxCalculator {
	
     public double taxAmount;

     public void calculateGSTTax(TaxCalculation taxCalculation, double productPrice) {
	   this.taxAmount = taxCalculation.calculateTax(productPrice);
     }
}

StrategyDesignPatternDemo.java is a demo class to show the Strategy design outcome. So in your code implemention you just need to invoke above class - GSTTaxCalculator method using object and pass TaxCalculation implemented class object and also to pass the item price, it's just an example. We need to implement the Strategy Design pattern as per your requirement and scenario's.

StrategyDesignPatternDemo.java

public class StrategyDesignPatternDemo {
	
	public static void main(String[] args) {
		
		GSTTaxCalculator gstTaxCalculator = new GSTTaxCalculator();
		
		gstTaxCalculator.calculateGSTTax(new FivePercentageTax(), 250);
		System.out.println("GST tax for 5% slab - "+gstTaxCalculator.taxAmount);
		
		gstTaxCalculator.calculateGSTTax(new TwelvePercentageTax(), 250);
		System.out.println("GST tax for 12% slab - "+gstTaxCalculator.taxAmount);
		
		gstTaxCalculator.calculateGSTTax(new EighteenPercentageTax(), 250);
		System.out.println("GST tax for 18% slab - "+gstTaxCalculator.taxAmount);
		
		gstTaxCalculator.calculateGSTTax(new TwentyEightPercentageTax(), 250);
		System.out.println("GST tax for 28% slab - "+gstTaxCalculator.taxAmount);
	}
}

Output:- GST tax for 5% slab - 12.5
              GST tax for 12% slab - 30.0
              GST tax for 18% slab - 45.0
              GST tax for 28% slab - 70.0


Thank you for visiting the blog.

No comments:

Post a Comment