Using the strategy pattern to change object behavior
The strategy pattern can sometimes be called a policy pattern because it establishes precise steps for runtime execution in a particular situation or state. This pattern is a part of the GoF’s book.
Motivation
The strategy pattern represents a family of algorithms where each one is properly encapsulated. It defines the interchangeability of algorithms to which a particular object can respond. This strategy allows the algorithm to change independently of the clients using it and allows the client to choose the most appropriate one on the fly. In other words, the code allows the client to attach various strategy objects that affect the behavior of the program.
Finding it in the JDK
The strategy pattern is another pattern commonly used without being aware of its use. The Collection
framework from the java.base
module and the java.util
package implements the Comparator
class. This class is often used for sorting purposes...