Enhancing Code Flexibility with the Strategy Design Pattern

The Problem :

Consider a scenario where a developer needs to implement three sorting algorithms: Bubble Sort, Merge Sort, and Quick Sort. Without employing the Strategy Design Pattern, the code structure might look like this:

pubic class BubbleSort
    public void sort(int[] arr)
    {
        //implementation of Bubble Sort
    }
}
pubic class MergeSort{
    public void sort(int[] arr)
    {
        //implementation of Merge Sort
    }
}
pubic class QuickSort{
    public void sort(int[] arr)
    {
        //implementation of Quick Sort
    }
}


public class Sorter {
    private BubbleSort bubbleSort;
    private MergeSort mergeSort;
    private QuickSort quickSort;


    public Sorter() {
        bubbleSort = new BubbleSort();
        mergeSort = new MergeSort();
        quickSort = new QuickSort();
    }


    public void sortUsingBubbleSort(int[] arr) {
        bubbleSort.sort(arr);
    }


    public void sortUsingMergeSort(int[] arr) {
        mergeSort.sort(arr);
    }


    public void sortUsingQuickSort(int[] arr) {
        quickSort.sort(arr);
    }
}


public class Main {
    public static void main(String[] args) {
        int[] arr = {3, 2, 4, 5, 1, 0, 6};
        Sorter sorter = new Sorter();


        sorter.sortUsingBubbleSort(arr);
        sorter.sortUsingMergeSort(arr);
        sorter.sortUsingQuickSort(arr);
    }
}

{        

Issues :

  1. Increased Class Complexity: As more sorting algorithms are added, the Sorter class becomes more complex, making it harder to maintain and understand.
  2. Hardcoded Sorting Algorithms: The specific sorting algorithms are hardcoded within the Sorter class. Adding or modifying a sorting algorithm requires changes directly to the Sorter class, violating the Open-Closed Principle.
  3. Client Code Coupling: The client code (i.e., Main class) needs to be aware of the individual sorting classes, leading to tighter coupling and reduced flexibility in selecting different sorting algorithms.

Solution :

public interface sortStrategy
    void sort(int arr[]);
}


public class BubbleSort implements sortStrategy{
    @Override
    void sort(int arr[]){
        //implement bubble sort
    }
}


public class MergeSort implements sortStrategy{
    @Override
    void sort(int arr[]){
        //implement Merge sort
    }
}
public class QuickSort implements sortStrategy{
    @Override
    void sort(int arr[]){
        //implement Quick sort
    }
}


public class Sorter
{
    private SortStrategy sortStrategy;


    public void setSortStrategy(SortStrategy strategy)
    {
        this.sortStrategy=strategy;
    }


    public void sortArray(int []arr)
    {
        sorter.sort(arr);
    }
}




public class Main{
    public static void main(String[] args)
    {
        int[] arr={3,2,4,5,1,0,6};
        Sorter sorter=new Sorter();


        sorter.setSortStrategy(new BubbleSort());
        sorter.sortArray(arr);


        sorter.setSortStrategy(new MergeSort());
        sorter.sortArray(arr);


        sorter.setSortStrategy(new QuickSort());
        sorter.sortArray(arr);
    }
}
        

Advantages of Strategy Design Pattern :

  1. Reduced Class Complexity: The Sorter class now only needs to hold a reference to the SortStrategy interface, simplifying the code structure and making maintenance easier.
  2. Flexible Sorting Algorithms: The Strategy Design Pattern allows us to add or modify sorting algorithms by implementing the SortStrategy interface. The Sorter class remains unchanged, adhering to the Open-Closed Principle.
  3. Decoupling Client Code: The client code (i.e., Main class) is now decoupled from the specific sorting algorithms. It interacts with the Sorter class using the SortStrategy interface, making it easy to switch between different sorting algorithms without modifying the client code.

Conclusion :

The Strategy Design Pattern is a powerful tool for achieving flexibility and maintainability in software development. By encapsulating algorithms in separate classes and selecting them dynamically through interfaces, we can significantly improve code organization and reusability. As we have seen through our example, the Strategy Design Pattern enhances code quality, adheres to design principles, and ultimately leads to more efficient, adaptable, and elegant solutions.

To view or add a comment, sign in

More articles by Pradeep Singh Rasaputra

Others also viewed

Explore content categories