Skip to content

Commit 2f69856

Browse files
authored
documentation for strategy pattern
1 parent c62dfa5 commit 2f69856

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
11
# Strategy
22

3-
The Strategy Pattern defines a family of algorithms, encapsulates each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
3+
The Strategy Pattern
4+
* defines a family of algorithms,
5+
* encapsulates each one, and
6+
* make them interchangeable.
7+
Strategy lets the algorithm vary independently from clients that use it.
8+
9+
![Duck App with Strategy Pattern](/Diagrams/Strategy.png)
10+
11+
**Usage**
12+
```cs
13+
Duck mallard = new MallardDuck();
14+
mallard.PerformQuack();
15+
mallard.PerformFly();
16+
17+
// change the flying behavior dynamically
18+
Duck model = new ModelDuck();
19+
model.PerformFly(); // default behavior
20+
model.FlyBehavior = new FlyRocketPowered(); // set a different flying behavior at runtime
21+
model.PerformFly();
22+
```
23+
24+
25+
## Common Structure
26+
27+
![Common structure of strategy pattern](http://www.dofactory.com/images/diagrams/net/strategy.gif)
28+
29+
* Strategy (SortStrategy)
30+
* declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
31+
* ConcreteStrategy (QuickSort, ShellSort, MergeSort)
32+
* implements the algorithm using the Strategy interface
33+
* Context (SortedList)
34+
* is configured with a ConcreteStrategy object
35+
* maintains a reference to a Strategy object
36+
* may define an interface that lets Strategy access its data.
37+
38+
_[Source: http://www.dofactory.com/net/strategy-design-pattern]_

0 commit comments

Comments
 (0)