-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite_pattern.html
89 lines (87 loc) · 2.23 KB
/
composite_pattern.html
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
<!DOCTYPE html>
<html>
<head>
<title>composite Pattern</title>
</head>
<body>
Look at the embedded javascript and see the console.log
<script type="text/javascript">
//component
class Employee{
constructor (name, position, progress){
this.name =name;
this.position =position;
this.progress =progress;
}
getProgress(){
}
}
// Leaf
class Developers extends Employee{
constructor(name, position, progress){
//same like as in super class
super(name, position, progress);
}
getProgress(){
return this.progress;
}
}
//Leaf subclass
class FreeLanceDev extends Employee{
constructor(name,position,progress){
super(name,position,progress)
}
getProgress(){
return this.progress()
}
}
//composite subclass
class DevTeamLead extends Employee{
constructor (name, position){
super(name, position);
this.teamMembers =[];
}
addMember(employee){
this.teamMembers.push(employee);
}
removeMember(employee){
for(var i=0; i<this.teamMembers.length; i++){
if(this.teamMembers[i] == employee){
this.teamMembers.splice(i,1)
}
}
return this.teamMembers
}
getProgress(){
for(var i=0; i<this.teamMembers.length; i++){
console.log(this.teamMembers[i].getProgress())
}
}
showTeam(){
for(var i=0; i<this.teamMembers.length; i++){
console.log(this.teamMembers[i].name)
}
}
}
const seniorDev = new Developers("Rachel","Senior Developer","60%")
const juniorDev = new Developers("Joey","Junior Developer", "50%")
const teamLead = new DevTeamLead("Regina", "Dev Team Lead","90%")
teamLead.addMember(seniorDev)
teamLead.addMember(juniorDev)
console.log("Team members list:")
teamLead.showTeam()
console.log("Get Team members progress:")
teamLead.getProgress()
console.log("Removing Rachel from team:")
teamLead.removeMember(seniorDev)
console.log("Updated team members list:")
teamLead.showTeam()
const freelanceDev = new Developers("Ross", "Free Lancer", "80%")
console.log("Get freelance developer's progress:")
console.log(freelanceDev.getProgress())
console.log(teamLead)
teamLead.showTeam()
console.log(teamLead.getProgress())
</script>
</body>
</html>