-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.js
98 lines (80 loc) · 2.84 KB
/
strategy.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Strategy Pattern
// allow to create a family of algorithms , encapsulate each one , and make them interchangeable , strategy lets the algorithm vary independently from clients that use it
// mostaly used in building plugin in mechanism when building extensible applications , for example , a plugin for a text editor , a plugin for a web browser , a plugin for a video player , etc
// a real world example is of a video player , a video player can play different types of videos , for example , mp4 , avi , mkv , etc , and each type of video has a different way of playing , for example , mp4 videos are played using the mp4 player , avi videos are played using the avi player , mkv videos are played using the mkv player , etc , so we can use the strategy pattern to create a family of algorithms , encapsulate each one , and make them interchangeable , strategy lets the algorithm vary independently from clients that use it
class Player {
constructor() {
this.player = null;
}
setPlayer(player) {
this.player = player;
}
play() {
this.player.play();
}
}
class MP4Player {
play() {
// each have its own way of playing
console.log('Play MP4');
}
}
class AVIPlayer {
play() {
console.log('Play AVI');
}
}
class MKVPlayer {
play() {
console.log('Play MKV');
}
}
function test() {
const player = new Player();
const mp4Player = new MP4Player();
player.setPlayer(mp4Player);
player.play();
player.setPlayer(new AVIPlayer());
player.play();
player.setPlayer(new MKVPlayer());
player.play();
}
test();
// a navigation app that navigate cars , walking , and bikes , each one has its own way of navigating , for example , cars navigate using the car navigator , walking navigate using the walking navigator , bikes navigate using the bike navigator , etc , so we can use the strategy pattern to create a family of algorithms , encapsulate each one , and make them interchangeable , strategy lets the algorithm vary independently from clients that use it
class Navigator {
constructor() {
this.navigator = null;
}
setNavigator(navigator) {
this.navigator = navigator;
}
navigate() {
this.navigator.navigate();
}
}
class CarNavigator {
navigate() {
console.log('Navigate Car');
}
}
class WalkingNavigator {
navigate() {
console.log('Navigate Walking');
}
}
class BikeNavigator {
navigate() {
console.log('Navigate Bike');
}
}
function test2() {
const navigator = new Navigator();
const carNavigator = new CarNavigator();
navigator.setNavigator(carNavigator);
navigator.navigate();
navigator.setNavigator(new WalkingNavigator());
navigator.navigate();
navigator.setNavigator(new BikeNavigator());
navigator.navigate();
}
test2();