Strategy design pattern in java

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.

Related Posts

  • 24 April

    Singleton design pattern in java

    Table of ContentsSingleton classEager initializationLazy initializationThread safe SingletonDouble check lockingBill Pugh Singleton ImplementationCreate singleton using EnumTesting singleton with ReflectionTesting singleton with SerializationTesting singleton with Cloning. In this post, we will see about Singleton design pattern in java. Singleton design pattern is one of the simplest design pattern but singleton property can be broken using multithreading, […]

  • 14 July

    How to use connection pool using service locator design Pattern in java

    In this post, you will learn about the Connection Pool. This post is intended by java application development experts for entire development community to let people learn and acknowledge about Connection Pool and how to use its object using Service Locator design pattern. Introduction Connection pool is a mechanism which applicable for all technologies in […]

  • 01 March

    Template method design pattern in java

    Table of ContentsExample:Used in java API:Source code: CodeProjectTemplate method pattern is a behavioral design pattern which provide base method for algorithm,called template method which defers some of its steps to subclasses So algorithm structure is same but some of its steps can be redefined by subclasses according to context. Template means Preset format like HTML […]

  • 27 February

    Observer design pattern in java

    Table of ContentsWhen to use it: UML diagram for observer design pattern:Components:Java in-built API for observer pattern:Example:Using java inbuilt APIs:Some important points about observer pattern:Source code: As the name suggests it is used for observing some objects.Observer watch for any change in state or property of subject.Suppose you are interested in particular object and want to […]

  • 29 September

    Builder design pattern in java

    Table of ContentsGeneric UML diagram for builder design pattern:Elements:When to use it:WorkFlow:Example: Builder design pattern allows to create complex object step by step and also enforces a process to create an object as a finished product.Construction of object should be such that same construction process can create different representations.Director controls the construction of object and […]

  • 28 September

    Proxy design pattern in java

    Table of ContentsWhen to use it:UML diagram for proxy design pattern:Elements:WorkFlow:Example:Source code: CodeProjectProxy design pattern allows you to create a wrapper class over real object.Wrapper class which is proxy,controls access to real object so in turn you can add extra functionalities to real object without changing real object’s code. As described by GoF: “Provide a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.