1
1
// Incorrecto
2
2
3
- /* class Entity {
4
- constructor(name, attackDamage, health ) {
3
+ /* class Animal {
4
+ constructor(name) {
5
5
this.name = name
6
- this.attackDamage = attackDamage
7
- this.health = health
8
- }
9
- move() {
10
- console.log(`${this.name} moved`)
11
- }
12
- attack(targetEntity) {
13
- console.log(`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`)
14
- targetEntity.takeDamage(this.attackDamage)
6
+ if (new.target === Animal) {
7
+ throw new Error('Animal es una interface y no puede ser instanciada')
8
+ }
9
+ if (!this.eat) {
10
+ throw new Error('Debe implimentar el método eat')
11
+ }
12
+ if (!this.fly) {
13
+ throw new Error('Debe implimentar el método fly')
14
+ }
15
+ if (!this.swim) {
16
+ throw new Error('Debe implimentar el método swim')
17
+ }
15
18
}
16
- takeDamage(amount) {
17
- this.health -= amount
18
- console.log(`${this.name} has ${this.health} remaining`)
19
- }
20
- }
21
-
22
- class Character extends Entity {
23
-
24
19
}
25
20
26
- class Wall extends Entity {
27
- constructor(name, health ) {
28
- super( name, 0, health )
21
+ class Fish extends Animal {
22
+ eat( ) {
23
+ console.log(`${this. name} is eating` )
29
24
}
30
- move () {
31
- return null
25
+ swim () {
26
+ console.log(`${this.name} is swimming`)
32
27
}
33
- attack () {
34
- return null
28
+ fly () {
29
+ console.error("ERROR! Fishes can't fly")
35
30
}
36
31
}
37
32
38
- class Turret extends Entity {
39
- constructor(name, attackDamage ) {
40
- super( name, attackDamage, -1 )
33
+ class Bird extends Animal {
34
+ eat( ) {
35
+ console.log(`${this. name} is eating` )
41
36
}
42
- move () {
43
- return null
37
+ swim () {
38
+ console.error("ERROR! Birds can't swim");
44
39
}
45
- takeDamage () {
46
- return null
40
+ fly () {
41
+ console.log(`${this.name} is flying`)
47
42
}
48
43
}
49
44
50
- const turret = new Turret('Turret', 5)
51
- const character = new Character('Character', 3, 100)
52
- const wall = new Wall('Wall', 100)
45
+ const bird = new Bird('Titi the Parrot');
46
+ bird.swim(); // ERROR! Birds can't swim
53
47
54
- turret.attack(character)
55
- character.move()
56
- character.attack(wall) */
48
+ const fish = new Fish('Neo the Dolphin');
49
+ fish.fly(); // ERROR! Fishes can't fly
50
+ */
51
+ // Correcto
57
52
58
- //Correcto
59
-
60
- class Entity {
53
+ class Swimmer {
61
54
constructor ( name ) {
62
55
this . name = name
56
+ if ( new . target === Swimmer ) {
57
+ throw new Error ( 'Swimmer es una interface y no puede ser instanciada' )
58
+ }
59
+ if ( ! this . swim ) {
60
+ throw new Error ( 'Debe implimentar el método swim' )
61
+ }
63
62
}
64
63
}
65
64
66
- const mover = {
67
- move ( ) {
68
- console . log ( `${ this . name } moved` )
65
+ class Flyer {
66
+ constructor ( name ) {
67
+ this . name = name
68
+ if ( new . target === Flyer ) {
69
+ throw new Error ( 'Flyer es una interface y no puede ser instanciada' )
70
+ }
71
+ if ( ! this . fly ) {
72
+ throw new Error ( 'Debe implimentar el método eat' )
73
+ }
69
74
}
70
75
}
71
76
72
- const attacker = {
73
- attack ( targetEntity ) {
74
- console . log ( `${ this . name } attacked ${ targetEntity . name } for ${ this . attackDamage } damage` )
75
- targetEntity . takeDamage ( this . attackDamage )
76
- }
77
- }
77
+ // Implement interfaces for specific types of animals
78
78
79
- const hasHealth = {
80
- takeDamage ( amount ) {
81
- this . health -= amount
82
- console . log ( `${ this . name } has ${ this . health } remaining` )
79
+ class Bird extends Flyer {
80
+ constructor ( name ) {
81
+ super ( name ) ;
83
82
}
84
- }
85
-
86
- class Character extends Entity {
87
- constructor ( name , attackDamage , health ) {
88
- super ( name )
89
- this . attackDamage = attackDamage
90
- this . health = health
83
+ fly ( ) {
84
+ console . log ( `${ this . name } is flying` ) ;
91
85
}
92
- }
93
-
94
- Object . assign ( Character . prototype , mover )
95
- Object . assign ( Character . prototype , attacker )
96
- Object . assign ( Character . prototype , hasHealth )
97
-
98
- class Wall extends Entity {
99
- constructor ( name , health ) {
100
- super ( name )
101
- this . health = health
86
+ eat ( ) {
87
+ console . log ( `${ this . name } is eating` ) ;
102
88
}
103
89
}
104
90
105
- Object . assign ( Wall . prototype , hasHealth )
106
-
107
- class Turret extends Entity {
108
- constructor ( name , attackDamage ) {
109
- super ( name )
110
- this . attackDamage = attackDamage
91
+ class Fish extends Swimmer {
92
+ constructor ( name ) {
93
+ super ( name ) ;
94
+ }
95
+ swim ( ) {
96
+ console . log ( `${ this . name } is swimming` ) ;
97
+ }
98
+ eat ( ) {
99
+ console . log ( `${ this . name } is eating` ) ;
111
100
}
112
101
}
113
102
114
- Object . assign ( Turret . prototype , attacker )
103
+ // Usage
104
+
105
+ const bird = new Bird ( 'Titi the Parrot' ) ;
106
+ bird . fly ( ) ; // Titi the Parrot is flying
107
+ bird . eat ( ) ; // Titi the Parrot is eating
115
108
116
- const turret = new Turret ( 'Turret' , 5 )
117
- const character = new Character ( 'Character' , 3 , 100 )
118
- const wall = new Wall ( 'Wall' , 100 )
109
+ console . log ( '\n' ) ;
119
110
120
- turret . attack ( character )
121
- character . move ( )
122
- character . attack ( wall )
111
+ const fish = new Fish ( 'Neo the Dolphin' ) ;
112
+ fish . swim ( ) ; // Neo the Dolphin is swimming
113
+ fish . eat ( ) ; // Neo the Dolphin is eating
0 commit comments