1+ // Incorrecto
2+
3+ /* class Animal {
4+ constructor(name) {
5+ this.name = name
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+ }
18+ }
19+ }
20+
21+ class Fish extends Animal {
22+ eat() {
23+ console.log(`${this.name} is eating`)
24+ }
25+ swim() {
26+ console.log(`${this.name} is swimming`)
27+ }
28+ fly() {
29+ console.error("ERROR! Fishes can't fly")
30+ }
31+ }
32+
33+ class Bird extends Animal {
34+ eat() {
35+ console.log(`${this.name} is eating`)
36+ }
37+ swim() {
38+ console.error("ERROR! Birds can't swim");
39+ }
40+ fly() {
41+ console.log(`${this.name} is flying`)
42+ }
43+ }
44+
45+ const bird = new Bird('Titi the Parrot');
46+ bird.swim(); // ERROR! Birds can't swim
47+
48+ const fish = new Fish('Neo the Dolphin');
49+ fish.fly(); // ERROR! Fishes can't fly
50+ */
51+ // Correcto
52+
53+ class Swimmer {
54+ constructor ( name ) {
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+ }
62+ }
63+ }
64+
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+ }
74+ }
75+ }
76+
77+ // Implement interfaces for specific types of animals
78+
79+ class Bird extends Flyer {
80+ constructor ( name ) {
81+ super ( name ) ;
82+ }
83+ fly ( ) {
84+ console . log ( `${ this . name } is flying` ) ;
85+ }
86+ eat ( ) {
87+ console . log ( `${ this . name } is eating` ) ;
88+ }
89+ }
90+
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` ) ;
100+ }
101+ }
102+
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
108+
109+ console . log ( '\n' ) ;
110+
111+ const fish = new Fish ( 'Neo the Dolphin' ) ;
112+ fish . swim ( ) ; // Neo the Dolphin is swimming
113+ fish . eat ( ) ; // Neo the Dolphin is eating
114+
115+ // Extra
116+
117+ class Printer {
118+ constructor ( ) {
119+ if ( new . target === Printer ) {
120+ throw new Error ( "Printer es una interface no puedes instanciarla." )
121+ }
122+ if ( ! this . print ) {
123+ throw new Error ( "Debes implementar el método print" )
124+ }
125+ }
126+ }
127+
128+ class BlackAndWhitePrinter extends Printer {
129+ print ( ) {
130+ console . log ( "Se imprime en blanco y negro." )
131+ }
132+ }
133+
134+ class ColorPrinter extends Printer {
135+ print ( ) {
136+ console . log ( "Se imprime a color." )
137+ }
138+ }
139+
140+ const sendingFax = {
141+ sendFax ( ) {
142+ console . log ( "Enviando fax." )
143+ }
144+ }
145+
146+ const scanner = {
147+ scanning ( ) {
148+ console . log ( "Escaneando documento." )
149+ }
150+ }
151+
152+ Object . assign ( ColorPrinter . prototype , sendingFax )
153+ Object . assign ( ColorPrinter . prototype , scanner )
154+
155+ const multifuncional = new ColorPrinter ( )
156+ multifuncional . print ( )
157+ multifuncional . sendFax ( )
158+ multifuncional . scanning ( )
159+
160+ function testInterface ( object , interface ) {
161+ const prototypeProperties = Object . getOwnPropertyNames ( interface . prototype )
162+
163+ for ( const method of prototypeProperties ) {
164+ if ( ! object [ method ] || typeof object [ method ] !== 'function' ) {
165+ throw new Error ( `La clase no implementa el método ${ method } de la interface.` )
166+ }
167+ }
168+ }
169+ testInterface ( multifuncional , Printer )
0 commit comments