Skip to content

Commit e7133a6

Browse files
committed
#29 - python y javascript
1 parent 3d75efd commit e7133a6

File tree

2 files changed

+490
-0
lines changed

2 files changed

+490
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)"
4+
* y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta.
5+
*
6+
* DIFICULTAD EXTRA (opcional):
7+
* Crea un gestor de impresoras.
8+
* Requisitos:
9+
* 1. Algunas impresoras sólo imprimen en blanco y negro.
10+
* 2. Otras sólo a color.
11+
* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
12+
* Instrucciones:
13+
* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
14+
* 2. Aplica el ISP a la implementación.
15+
* 3. Desarrolla un código que compruebe que se cumple el principio.
16+
*/
17+
18+
// Forma incorrecta de aplicar el principio ISP
19+
20+
class Vehicle {
21+
startEngine() {
22+
throw new Error("Method 'startEngine()' must be implemented.");
23+
}
24+
stopEngine() {
25+
throw new Error("Method 'stopEngine()' must be implemented.");
26+
}
27+
fly() {
28+
throw new Error("Method 'fly()' must be implemented.");
29+
}
30+
drive() {
31+
throw new Error("Method 'drive()' must be implemented.");
32+
}
33+
sail() {
34+
throw new Error("Method 'sail()' must be implemented.");
35+
}
36+
}
37+
38+
class Car extends Vehicle{
39+
startEngine() {
40+
console.log('Car engine started');
41+
}
42+
43+
stopEngine() {
44+
console.log('Car engine stopped');
45+
}
46+
47+
fly() {
48+
throw new Error('Cars cannot fly');
49+
}
50+
51+
drive() {
52+
console.log('Car is driving');
53+
}
54+
55+
sail() {
56+
throw new Error('Cars cannot sail');
57+
}
58+
}
59+
60+
const car1 = new Car();
61+
car1.startEngine();
62+
car1.fly();
63+
64+
// Forma correcta de aplicar el principio ISP
65+
66+
class EnginePowered{
67+
startEngine(){
68+
throw new Error("Method 'startEngine()' must be implemented.");
69+
}
70+
stopEngine(){
71+
throw new Error("Method 'stopEngine()' must be implemented.");
72+
}
73+
}
74+
class Drivable{
75+
drive(){
76+
throw new Error("Method 'drive()' must be implemented.");
77+
}
78+
}
79+
class Flyable{
80+
fly(){
81+
throw new Error("Method 'fly()' must be implemented.");
82+
}
83+
}
84+
class Sailable{
85+
sail(){
86+
throw new Error("Method 'sail()' must be implemented.");
87+
}
88+
}
89+
90+
91+
class CarEngine extends EnginePowered{
92+
startEngine() {
93+
console.log('Car engine started');
94+
}
95+
stopEngine() {
96+
console.log('Car engine stopped');
97+
}
98+
}
99+
100+
class CarDrive extends Drivable{
101+
drive(){
102+
console.log('Car is driving');
103+
}
104+
}
105+
106+
class Car{
107+
constructor(){
108+
this.engine = new CarEngine();
109+
this.driving = new CarDrive();
110+
}
111+
startEngine(){
112+
this.engine.startEngine();
113+
}
114+
stopEngine(){
115+
this.engine.stopEngine();
116+
}
117+
drive(){
118+
this.driving.drive();
119+
}
120+
}
121+
122+
class PlaneEngine extends EnginePowered{
123+
startEngine(){
124+
console.log('Plane engine started');
125+
}
126+
stopEngine(){
127+
console.log('Plane engine stopped');
128+
}
129+
}
130+
131+
class PlaneFly extends Flyable{
132+
fly(){
133+
console.log("Plane is flying")
134+
}
135+
}
136+
137+
class Plane{
138+
constructor(){
139+
this.engine = new PlaneEngine();
140+
this.flying = new PlaneFly();
141+
}
142+
startEngine(){
143+
this.engine.startEngine();
144+
}
145+
stopEngine(){
146+
this.engine.stopEngine();
147+
}
148+
fly(){
149+
this.flying.fly();
150+
}
151+
}
152+
153+
154+
const car = new Car();
155+
156+
car.startEngine();
157+
car.drive();
158+
car.stopEngine();
159+
160+
const plane = new Plane();
161+
162+
plane.startEngine();
163+
plane.fly();
164+
plane.stopEngine();
165+
166+
167+
//////////////// --------------------------------------- EXTRA ------------------------------- //////////////////////
168+
169+
170+
class BlackWhitePrint{
171+
printBlWh(){
172+
throw new Error("Method 'printBlWh()' must be implemented.");
173+
}
174+
}
175+
class ColorPrint{
176+
printColor(){
177+
throw new Error("Method 'printColor()' must be implemented.");
178+
}
179+
}
180+
class ScannerPrint{
181+
scan(){
182+
throw new Error("Method 'scan()' must be implemented.");
183+
}
184+
}
185+
class FaxPrint{
186+
sendFax(dest){
187+
throw new Error("Method 'sendFax()' must be implemented.");
188+
}
189+
}
190+
191+
// Implementaciones para una impresoar Epson
192+
class EpsonBlackWhite extends BlackWhitePrint{
193+
printBlWh(){
194+
console.log("Impresion en blanco y negro")
195+
}
196+
}
197+
class EpsonColor extends ColorPrint{
198+
printColor(){
199+
console.log("Impresion a colores")
200+
}
201+
}
202+
class EpsonScanner extends ScannerPrint{
203+
scan(){
204+
console.log("Escaneo disponible")
205+
}
206+
}
207+
208+
class Epson{
209+
constructor(){
210+
this.blackWhite = new EpsonBlackWhite();
211+
this.colores = new EpsonColor();
212+
this.escaner = new EpsonScanner();
213+
}
214+
printBlWh(){
215+
this.blackWhite.printBlWh();
216+
}
217+
printColor(){
218+
this.colores.printColor();
219+
}
220+
scan(){
221+
this.escaner.scan();
222+
}
223+
}
224+
225+
// Implementaciones para una impresora Cannon
226+
class CannonBlackWhite extends BlackWhitePrint{
227+
printBlWh(){
228+
console.log("Impresion en blanco y negro")
229+
}
230+
}
231+
class CannonFax extends FaxPrint{
232+
sendFax(dest){
233+
console.log(`Enviando documento a ${dest}`)
234+
}
235+
}
236+
237+
class Cannon{
238+
constructor(){
239+
this.blackWhite = new CannonBlackWhite();
240+
this.sendTo = new CannonFax();
241+
}
242+
printBlWh(){
243+
this.blackWhite.printBlWh();
244+
}
245+
sendFax(dest){
246+
this.sendTo.sendFax(dest);
247+
}
248+
}
249+
250+
const epson = new Epson();
251+
epson.printBlWh();
252+
epson.printColor(),
253+
epson.scan();
254+
255+
const cannon = new Cannon();
256+
cannon.printBlWh();
257+
cannon.sendFax("juan4512");

0 commit comments

Comments
 (0)