-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathComposition-Vs-Inheritance.js
More file actions
248 lines (220 loc) · 6.71 KB
/
Copy pathComposition-Vs-Inheritance.js
File metadata and controls
248 lines (220 loc) · 6.71 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* 💡"JavaScript-with-JC"
👉 Composition Vs Inheritance
Composition is a pattern to write a reusable and more flexible code.
Using the Composition pattern, you design your models for what they can do,
while using inheritance pattern, you create your models for what they are.
💡 Why to use Composition over Inheritance ?
👉 Composition allows code-reusability.
👉 Composition follows Dry ( Do Not Repeat ).
👉 Composition is more flexible.
👉 Composition helps to write loosely coupled code.
*/
// Let's first take an example of Inheritance
// common Parent class for all the players
class Player {
constructor(name, role, country) {
// instance members
this.name = name;
this.role = role;
this.country = country;
}
// prototype members - common tasks which can be perform by any player in a team
fielding() {
console.log(`${this.name} can do fielding.`);
}
running() {
console.log(`${this.name} can do running.`);
}
}
// 1) Batsman class extends from Player Base class
class Batsman extends Player {
// prototype member
batting() {
// batting method created count 1
console.log(`${this.name} can do batting.`);
}
}
const virat = new Batsman("Virat Kohli", "Batsman", "India");
console.log(virat); // Batsman { name: 'Virat Kohli', role: 'Batsman', country: 'India' }
virat.fielding(); // Virat Kohli can do fielding.
virat.running(); // Virat Kohli can do running.
virat.batting(); // Virat Kohli can do batting.
// 2) Bowler class extends from Player Base class
class Bowler extends Player {
// prototype member
bowling() {
// bowling method created count 1
console.log(`${this.name} can do bowling.`);
}
}
const bumrah = new Bowler("Jasprit Bumrah", "Bowler", "India");
console.log(bumrah); // Bowler { name: 'Jasprit Bumrah', role: 'Bowler', country: 'India' }
bumrah.fielding(); // Jasprit Bumrah can do fielding.
bumrah.running(); // Jasprit Bumrah can do running.
bumrah.bowling(); // Jasprit Bumrah can do bowling.
// 3) AllRounder class extends from Player Base class
class AllRounder extends Player {
// Here we are repeating methods, Not following DRY ( do not repeat )
// batting and bowling method repeated here ( already created in Batsman and Bowler )
batting() {
// batting method created count 2
console.log(`${this.name} can do batting.`);
}
bowling() {
// bowling method created count 2
console.log(`${this.name} can do bowling.`);
}
}
const hardik = new AllRounder("Hardik Pandya", "All-Rounder", "India");
console.log(hardik); //AllRounder { name: 'Hardik Pandya', role: 'All-Rounder', country: 'India' }
hardik.fielding(); // Hardik Pandya can do fielding.
hardik.running(); // Hardik Pandya can do running.
hardik.batting(); // Hardik Pandya can do batting.
hardik.bowling(); // Hardik Pandya can do bowling.
// 4) Wicketkeeper class extends from Player Base class
class Wicketkeeper extends Player {
// Here we are repeating methods, Not following DRY ( do not repeat )
// batting method repeated here ( already created in Batsman and AllRounder)
batting() {
// batting method created count 3
console.log(`${this.name} can do batting.`);
}
wicketKeeping() {
console.log(`${this.name} can do wicket keeping.`);
}
}
const dinesh = new Wicketkeeper("Dinesh Karthik", "Wicket-Keeper", "India");
console.log(dinesh); // Wicketkeeper { name: 'Dinesh Karthik', role: 'Wicket-Keeper', country: 'India' }
dinesh.fielding(); // Dinesh Karthik can do fielding.
dinesh.running(); // Dinesh Karthik can do running.
dinesh.batting(); // Dinesh Karthik can do batting.
dinesh.wicketKeeping(); // Dinesh Karthik can do wicket keeping.
// So in above example, we have created batting method 3 times and bowling method 2 times ( same code repeated )
// So to avoid repeating code, Composition comes to rescue that follows D.R.Y
// Now Let's check How composition works ?
// We will create all the method at once, and use them for all the objects ( avoiding repeated methods )
const fielding = () => {
return {
fielding() {
console.log(`${this.name} can do fielding.`);
},
};
};
const running = () => {
return {
running() {
console.log(`${this.name} can do running.`);
},
};
};
const batting = () => {
return {
batting() {
console.log(`${this.name} can do batting.`);
},
};
};
const bowling = () => {
return {
bowling() {
console.log(`${this.name} can do bowling.`);
},
};
};
const wicketKeeping = () => {
return {
wicketKeeping() {
console.log(`${this.name} can do wicket keeping.`);
},
};
};
// createPlayer is common base function for all players
const createPlayer = (name, role, country) => {
return {
name,
role,
country,
...fielding(),
...running(),
};
};
// 1) createBatsman returning object extending common player with batting method
const createBatsman = (...playerInfo) => {
return {
...createPlayer(...playerInfo),
...batting(),
};
};
const rohit = createBatsman("Rohit Sharma", "Batsman", "India");
console.log(rohit);
/* output
{
name: 'Rohit Sharma',
role: 'Batsman',
country: 'India',
fielding: [Function: fielding],
running: [Function: running],
batting: [Function: batting]
}
*/
// 2) createBowler returning object extending common player with bowling method
const createBowler = (...playerInfo) => {
return {
...createPlayer(...playerInfo),
...bowling(),
};
};
const bhuvi = createBowler("Bhuvi Kumar", "Bolwer", "India");
console.log(bhuvi);
/* output
{
name: 'Bhuvi Kumar',
role: 'Bolwer',
country: 'India',
fielding: [Function: fielding],
running: [Function: running],
bowling: [Function: bowling]
}
*/
// 3) createAllRounder returning object extending common player with batting and bowling methods
const createAllRounder = (...playerInfo) => {
return {
...createPlayer(...playerInfo),
...batting(),
...bowling(),
};
};
const jadeja = createAllRounder("Ravindra Jadeja", "All-Rounder", "India");
console.log(jadeja);
/*
{
name: 'Ravindra Jadeja',
role: 'All-Rounder',
country: 'India',
fielding: [Function: fielding],
running: [Function: running],
batting: [Function: batting],
bowling: [Function: bowling]
}
*/
// 4) createWicketKeeper returning object extending common player with batting and wicketKeeping methods
const createWicketKeeper = (...playerInfo) => {
return {
...createPlayer(...playerInfo),
...batting(),
...wicketKeeping(),
};
};
const rishab = createWicketKeeper("Rishab Pant", "Wicket-Keeper", "India");
console.log(rishab);
/* output
{
name: 'Rishab Pant',
role: 'Wicket-Keeper',
country: 'India',
fielding: [Function: fielding],
running: [Function: running],
batting: [Function: batting],
wicketKeeping: [Function: wicketKeeping]
}
*/