Strategy
Intent
Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from
clients that use it.
Participants
-
Strategy -
defines the interface for the family of algorithms.
-
Concrete strategy -
provides an implementation of the strategy interface.
Each concrete strategy may implement the interface in different ways.
-
Context -
determines when to invoke the strategy.
Often also provides an interface on which strategies act.
Other Terms
-
push -
an option in which the context passes all necessary information to its
strategy through parameters.
-
pull -
an option in which the strategy invokes context methods to obtain all
necessary information.
Notes
Toolkits and frameworks often provide a number of concrete strategies in
addition to defining the strategy interface.
Examples
-
LayoutManager
is a strategy that is used in a very specific context.
-
FileFilter
is a strategy that can be used in several contexts.
-
FilenameFilter
is a strategy that can be used in several contexts.
-
Comparator
is a strategy that can be used in a variety of contexts.