11// Incorrecto
22
3- /* class Entity {
4- constructor(name, attackDamage, health ) {
3+ /* class Animal {
4+ constructor(name) {
55 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+ }
1518 }
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-
2419}
2520
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` )
2924 }
30- move () {
31- return null
25+ swim () {
26+ console.log(`${this.name} is swimming`)
3227 }
33- attack () {
34- return null
28+ fly () {
29+ console.error("ERROR! Fishes can't fly")
3530 }
3631}
3732
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` )
4136 }
42- move () {
43- return null
37+ swim () {
38+ console.error("ERROR! Birds can't swim");
4439 }
45- takeDamage () {
46- return null
40+ fly () {
41+ console.log(`${this.name} is flying`)
4742 }
4843}
4944
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
5347
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
5752
58- //Correcto
59-
60- class Entity {
53+ class Swimmer {
6154 constructor ( name ) {
6255 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+ }
6362 }
6463}
6564
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+ }
6974 }
7075}
7176
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
7878
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 ) ;
8382 }
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` ) ;
9185 }
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` ) ;
10288 }
10389}
10490
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` ) ;
111100 }
112101}
113102
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
115108
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' ) ;
119110
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