Advantages
(+)
-
Avoids compile-time implementation dependencies.
-
Clients refer to an interface
(
Strategy)
and are independent of an implementation.
-
Provides a flexible alternative to subclassing.
-
Subclassing provides a way to
change the algorithm of a class
(at compile-time).
When a subclass is instantiated,
its algorithm is fixed and can't be changed
for the life-time of the object.
-
Strategy provides a way
to change the algorithm
of an object (at run-time)
by delegating to different
Strategy objects.
-
Avoids conditional statements for switching
between algorithms.
-
Conditional statements
that switch between different algorithms
are replaced by delegating to different
Strategy objects.
-
"Code containing many conditional statements often indicates
the need to apply the Strategy pattern."
[GoF, p318]
Disadvantages
(–)
-
Can make the common
Strategy interface complex.
-
The
Strategy interface may get complex
because it must pass in the
needed data for all supported algorithms
(whether they are simple or complex;
see Implementation).
-
Requires that clients understand how strategies differ.
-
"The pattern has a potential drawback in that
a client must understand how Strategies
differ before it can select the appropriate one.
Clients might be exposed to implementation issues."
[GoF, p318]
-
Introduces an additional level of indirection.
-
Strategy achieves flexibility by introducing
an additional level of indirection
(clients delegate an algorithm to a
separate
Strategy object),
which makes clients dependent on
a Strategy
object.
-
This "can complicate a design and/or cost you some performance.
A design pattern should only be applied when the
flexibility it affords is actually needed."
[GoF, p31]