Strategy Pattern
Strategy Pattern
Context
- A class can benefit from different variants for an algorithm
- Clients sometimes want to replace standard algorithms with custom
versions
Solution
- Define an interface type that is an abstraction for the algorithm
- Actual strategy classes realize this interface type.
- Clients can supply strategy objects
- Whenever the algorithm needs to be executed, the context class
calls the appropriate methods of the strategy object
Strategy Pattern
Strategy Pattern: Layout Management
Name in
Design Pattern
|
Actual Name
(layout management)
|
Context
|
Container
|
Strategy
|
LayoutManager
|
ConcreteStrategy
|
a layout manager such as BorderLayout
|
doWork()
|
a method such as layoutContainer
|
Strategy Pattern: Sorting
Name in
Design Pattern
|
Actual Name
(sorting)
|
Context
|
Collections
|
Strategy
|
Comparator
|
ConcreteStrategy
|
a class that implements Comparator
|
doWork()
|
compare
|
Maintained by John Loomis,
last updated 25 February 2007