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 is the Purpose of Native Method in Java ?

What is the Difference Between Throw and Throws in Java ?

What is the Difference Between pass by reference and pass by valu ...

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

How Does Garbage Collection Work in Java ?