Skip to content

Commit 8362857

Browse files
committed
update Strategy pattern
1 parent ffcb0c9 commit 8362857

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

BehavioralPatterns/Strategy/README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@ The Strategy Pattern
44
* defines a family of algorithms,
55
* encapsulates each one, and
66
* make them interchangeable.
7+
78
Strategy lets the algorithm vary independently from clients that use it.
89

9-
![Duck App with Strategy Pattern](/Diagrams/Strategy.png)
10+
## Problem
1011

11-
**Usage**
12-
```cs
13-
Duck mallard = new MallardDuck();
14-
mallard.PerformQuack();
15-
mallard.PerformFly();
12+
- A class should be configured with an algorithm instead of implementing an algorithm directly.
13+
- An algorithm should be selected and exchanged at run-time.
1614

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-
```
15+
## Solution
2316

17+
- Define an interface `Strategy` for performing an algorithm and define separate classes that implement the `Strategy` interface and encapsulate an algorithm in different ways.
2418

2519
## Common Structure
2620

27-
![Common structure of strategy pattern](http://www.dofactory.com/images/diagrams/net/strategy.gif)
21+
![Common structure of strategy pattern](img/structure.jpg)
2822

2923
* Strategy (SortStrategy)
3024
* declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
@@ -35,4 +29,30 @@ model.PerformFly();
3529
* maintains a reference to a Strategy object
3630
* may define an interface that lets Strategy access its data.
3731

38-
_[Source: http://www.dofactory.com/net/strategy-design-pattern]_
32+
## Collaboration
33+
34+
- A context may pass all data required by the algorithm to the strategy when an algorithm is called.
35+
- A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter clients interact with the context exclusively.
36+
37+
## Benefits
38+
39+
## Drawbacks
40+
41+
## Example
42+
43+
![Duck App with Strategy Pattern](/Diagrams/Strategy.png)
44+
45+
**Usage**
46+
```cs
47+
Duck mallard = new MallardDuck();
48+
mallard.PerformQuack();
49+
mallard.PerformFly();
50+
51+
// change the flying behavior dynamically
52+
Duck model = new ModelDuck();
53+
model.PerformFly(); // default behavior
54+
model.FlyBehavior = new FlyRocketPowered(); // set a different flying behavior at runtime
55+
model.PerformFly();
56+
```
57+
58+
## Relations with Other Patterns
43.9 KB
Loading

0 commit comments

Comments
 (0)