We have learnt already about how to implement Command and Observer patterns using Java 8 Lambda Expression. Now, we would look into Strategy Pattern.
Strategy Pattern
The Problem Domain
The problem is an excerpt from the book Java 8 Lambdas by Richard Warburton.
We have a requirement of compressing a specific file using different compression methods. We need an efficient way to add extensions to add several compressions strategies so that we need not to violate the Open Closed Principle.
OOP way
Let’s first solve it the usual way.
Strategy
Concrete Strategy - GZIP Compression
Concrete Strategy - ZIP Compression
Client
Functional Way
You can assume that it’s gonna be simpler than the previous design patterns. We just have to write the client class to pass the concrete compression strategies as behaviors to Compressor constructor argument as it accepts the SAM (Single Abstract Method) Interface CompressionStrategy.
Client
Conclusion
Using Strategy Pattern, the user can change or alter the algorithms that the client application is using.