-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
36 lines (29 loc) · 791 Bytes
/
index.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
class StrategyContext {
private behavior: Behavior;
constructor(behavior: Behavior) {
this.behavior = behavior;
}
public setBehavior(behavior: Behavior) {
this.behavior = behavior;
}
public doAction(): string {
return this.behavior.run();
}
}
interface Behavior {
run(): string;
}
class FirstBehavior implements Behavior {
public run(): string {
return 'This is the first behavior.';
}
}
class SecondBehavior implements Behavior {
public run(): string {
return 'This is the second behavior.';
}
}
const strategyContext = new StrategyContext(new FirstBehavior());
console.log(strategyContext.doAction());
strategyContext.setBehavior(new SecondBehavior());
console.log(strategyContext.doAction());