-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.ts
41 lines (32 loc) · 1007 Bytes
/
strategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Define a generic algorythm and make implementations interchangable
Allows for flexibility in choise of strategy in given context
*/
module Strategy {
export interface IStrategy {
execute(): void;
}
export class StrategyOne implements IStrategy {
public execute(): void {
write("StrategyOne executed");
}
}
export class StrategyTwo implements IStrategy {
public execute(): void {
write("StrategyTwo executed");
}
}
export class Context {
private strategy: IStrategy;
constructor(strategy: IStrategy) {
this.strategy = strategy;
}
public executeStrategy(): void {
this.strategy.execute();
}
}
}
var context = new Strategy.Context(new Strategy.StrategyOne());
context.executeStrategy();
var otherContext = new Strategy.Context(new Strategy.StrategyTwo());
otherContext.executeStrategy();