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

What are the Uses of Optional in Java ?

How Will You Sort Objects by Natural Order in a Java List ?

Comparing HashSet and TreeSet Collections in Java

Enumeration and Iterator, Which One Has Better Performance in Jav ...

What are the Rules of Method Overloading and Method Overriding in ...