When Will You Use Strategy Design Pattern in Java ?

Strategy pattern is very useful for implementing a family of algorithms. It is a behavioral design pattern.

With Strategy pattern we can select the algorithm at runtime. We can use it to select the sorting strategy for data. We can use it to save files in different formats like- .txt, .csv, .jpg etc.

In Strategy pattern we create an abstraction, which is an interface through which clients interact with our system. Behind the abstraction we create multiple implementation of same interface with different algorithms.

For a client, at runtime we can vary the algorithm based on the type of request we have received.

So we use Strategy pattern to hide the algorithm implementation details from client.

In Java Collections.sort() method uses strategy design pattern.



You May Interest

How Does Web Module Work in Spring Framework ?

What are the Situations in Which You Choose HashSet or TreeSet in ...

What are the Main Uses of Singleton Design Pattern in Java ?

What are the Atomic Operations in Java ?

What is a Singleton Class in Java ?