Strategy design pattern in java

Previous
Next

In this post, we will see about Strategy design pattern in java.

Strategy design pattern allows us to change algorithm implementation at runtime.Strategy design pattern provides multiple algorithms and client can choose algorithm based on their needs with the help of composition.

Strategy design pattern example

Let’s understand this with the help of simple example.
Let’s say you want to implement multiple sorting algorithms.
You need to sort list based on input sorting type(Merge sort and Quick sort).Please note that there should be provision to add new sorting type in future.

Create an Enum called “SortingType.java” as below.

Create Service class named SortingManager as below:

Create a main class named “SortingMain.java” which will call SortingManager to sort list.

Output:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort

As you can see, this is simple code which is working fine.You have tested code and found out that you are able to sort list based on sorting type.
Class diagram without Strategy pattern
Now you need to create one more sorting type i.e. Heap sort.If you notice, you need to make changes as below:
1) You need to make changes in Enum SortingType.

2) You need to make changes in SortingManager class which you have already tested.

Now you can sort list using heap sirt as below.

Output:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort===================================
Sorting List based on Type
===================================
Sorting List using heap sort

As you can see, we have to modify at many places which we have already tested and we need to retest all the functionalities again.

Strategy pattern example

Let’s see how Strategy pattern will be able to solve the problem.
Create SortingManager.java as below.

Create an interface called SortingStrategy as below.

Create a class named “MergeStrategy.java” for sorting using merge sort.

Create a class named “QuickSortStrategy.java” for sorting using quick sort.

Let’s create a main class SortingMain.java now.

Output:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort

Strategy design pattern
Let’s say you want to sort using heap sort.You need to create below changes:
Create another class named “HeapSortStrategy.java” as below.

Change in SortingMain.java to add calling code.

Output:

===================================
Sorting List based on Type
===================================
Sorting List using merge sort===================================
Sorting List based on Type
===================================
Sorting List using quick sort===================================
Sorting List based on Type
===================================
Sorting using heap sort

As you can see, we did not make any changes SortingManager which was already tested.We just added new class “HeapSortStrategy” which enabled us to sort using Heap sort.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog