Thursday, 27 July 2023

Strategy Design Pattern in Java with Real-Time Example

     In this post, we will learn the Strategy Design Pattern with a real-world example. For all design pattern articles, refer to Design Patterns.

The Strategy Design Pattern is one of the behavioral design patterns. It is used when there are multiple algorithms available for a specific task, and the client decides which implementation to use at runtime. The Strategy pattern provides greater flexibility, extensibility, and maintainability by allowing algorithms to be selected dynamically.

There are many real-world use cases for the Strategy pattern, such as supporting different payment methods, implementing various sorting algorithms, and many more.

In this article, we will use a GST tax calculation example to understand the Strategy pattern in detail. GST rates vary depending on the type of product. Currently, there are four GST slabs: 5%, 12%, 18%, and 28%. Some products are GST-exempt (0% GST), but for simplicity, we will not consider that case in this example.

Instead of writing the tax calculation logic in multiple conditional statements or tightly coupling it to a single class, we will implement a separate strategy class for each GST slab. This approach makes the code more flexible, readable, reusable, and easier to extend. It also follows the Open/Closed Principle by allowing new tax strategies to be added without modifying existing code.

The first step is to create a common interface. Then, create a separate implementation class for each GST slab, with each class implementing 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 GSTTaxCalculator class is the main class that contains the calculateGSTTax() method. This method accepts an object of a class that implements the TaxCalculation interface. Using this object, it invokes the calculateTax() method, which executes the corresponding GST tax calculation strategy based on the implementation passed at runtime.

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 that demonstrates how the Strategy Design Pattern works. In your application, you simply need to create an instance of the GSTTaxCalculator class, invoke the calculateGSTTax() method, pass an object of a class that implements the TaxCalculation interface, and provide the item price as input.
This is just a simple example to illustrate the Strategy Design Pattern. In a real-world application, you should implement the pattern based on your business requirements and use case.

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