O'Reilly logo

Learning Java Functional Programming by Richard M Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the strategy pattern

The strategy pattern allows an algorithm to be selected at runtime based on the needs of the application. Instead of using the if type statements to select an algorithm, the algorithm's implementation is contained in classes that implement an interface depicting the desired operation. This allows the algorithm executed to vary depending on the client it is applied against.

The pattern does not use inheritance, but rather encapsulates the behavior in another class. This composition approach decouples the behavior from the classes that use the behavior. Changing the behavior does not affect the class that uses it.

Let's assume that a list of tasks needs to be processed. However, there are various task ordering algorithms ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required