Strategy Pattern in java

Category : Design Patterns | Sub Category : Behavioral Design Patterns | By Prasad Bonam Last updated: 2023-07-09 09:17:11 Viewed : 299

Strategy Pattern:

The Strategy pattern is a behavioral design pattern that enables selecting an algorithm at runtime from a family of interchangeable algorithms. It allows you to encapsulate different algorithms into separate classes and make them interchangeable without changing the client code that uses them. The Strategy pattern promotes flexibility, maintainability, and extensibility by allowing the behavior of an object to be determined dynamically. Here is an example of implementing the Strategy pattern in Java:

// Strategy interface interface SortingStrategy { void sort(int[] numbers); } // Concrete strategies class BubbleSortStrategy implements SortingStrategy { @Override public void sort(int[] numbers) { System.out.println("Sorting using Bubble Sort"); // Bubble sort implementation } } class QuickSortStrategy implements SortingStrategy { @Override public void sort(int[] numbers) { System.out.println("Sorting using Quick Sort"); // Quick sort implementation } } // Context class class Sorter { private SortingStrategy sortingStrategy; public void setSortingStrategy(SortingStrategy sortingStrategy) { this.sortingStrategy = sortingStrategy; } public void sort(int[] numbers) { sortingStrategy.sort(numbers); } } // Client code public class Client { public static void main(String[] args) { Sorter sorter = new Sorter(); SortingStrategy bubbleSortStrategy = new BubbleSortStrategy(); sorter.setSortingStrategy(bubbleSortStrategy); int[] numbers = { 5, 2, 8, 1, 9 }; sorter.sort(numbers); // Output: Sorting using Bubble Sort SortingStrategy quickSortStrategy = new QuickSortStrategy(); sorter.setSortingStrategy(quickSortStrategy); sorter.sort(numbers); // Output: Sorting using Quick Sort } }

In this example:

  • The SortingStrategy interface declares the strategy methods that different sorting algorithms should implement. In this case, it defines the sort() method.
  • The BubbleSortStrategy and QuickSortStrategy classes are concrete strategies that implement the SortingStrategy interface. They provide their own implementations of the sorting algorithm.
  • The Sorter class is the context class that uses a sorting strategy. It has a reference to a SortingStrategy object and delegates the sorting operation to it.
  • The client code in the Client class demonstrates the usage of the strategy pattern. It creates an instance of the Sorter class and sets different sorting strategies (BubbleSortStrategy and QuickSortStrategy) at runtime using the setSortingStrategy() method. It then calls the sort() method on the Sorter object, which internally delegates the sorting operation to the currently set strategy.

By using the Strategy pattern, you can decouple the algorithm implementation from the client code that uses it. It allows for dynamic selection of algorithms at runtime and enables easy addition or modification of strategies without affecting the client code. This pattern promotes code reusability, flexibility, and the ability to easily switch between different algorithms based on runtime conditions or user preferences.

Related Articles

Leave a Comment: