Origin: |
GoF 95 |
Reason: |
To allow the selection of an algorithm to vary by object and
over time.
|
Synopsis: |
Encapsulate related algorithms in classes that are subclasses
of a common class.
|
Example: |
Consider a program that generates a calendar.
Holidays displayed by the calendar must vary
by nation and/or religious group.
|
Solution: |
Client |
This class delegates an operation to an abstract class
or interface, the AbstractStrategy.
It does not know the actual object that
will execute the operation.
|
Calendar printer. |
AbstractStrategy |
Abstract class or interface that abstracts the operation
provided by all its subclasses, the ConcreteStrategies.
|
Defines method, isHoliday, that takes a day of the year. |
ConcreteStrategyi |
There are many classes that provide different implementations
of the operation specified by AbstractStrategy.
|
Specilizes method isHoliday for a specific country. |
|
See also: |
Template Method and
Factory Method
(structurally similar)
|