Design Patterns
|
Strategy
- References
- Example
-
References
-
Definition: (source: http://en.wikipedia.org/wiki/Strategy_pattern)
The strategy pattern (also known as the policy pattern) enables an algorithm's behavior to be selected at runtime.
The strategy pattern:
► defines a family of algorithms,
► encapsulates each algorithm, and
► makes the algorithms interchangeable within that family.
Strategy lets the algorithm vary independently from clients that use it.
-
Class diagram: (source: http://en.wikipedia.org/wiki/Strategy_pattern)
-
Example
-
The
Subject
:
-
The
Strategy
:
-
The
Concrete Strategy
(1):
-
The
Concrete Strategy
(2):
-
The
Strategy Context
:
-
A simple class to test the
Strategy
design pattern: