Skip to content

Commit e08435d

Browse files
authored
Merge pull request mouredev#4946 from Kenysdev/29.cs
#29 - c#
2 parents 9c294fa + 2f37d45 commit e08435d

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Roadmap/29 - SOLID ISP/c#/kenysdev.cs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
namespace exs28;
2+
/*
3+
╔══════════════════════════════════════╗
4+
║ Autor: Kenys Alvarado ║
5+
║ GitHub: https://github.com/Kenysdev ║
6+
║ 2024 - C# ║
7+
╚══════════════════════════════════════╝
8+
-----------------------------------------------------
9+
* SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP)
10+
-----------------------------------------------------
11+
- Una clase no debería estar obligada a implementar interfaces que no utiliza.
12+
Evitando crear grandes clases monolíticas.
13+
*/
14+
15+
// NOTA: Este ejemplo muestra el uso CORRECTO. Para suponer un ejemplo que viole el principio, sería.
16+
//       Imaginar todos los métodos siguientes, en una sola interfaz, entonces algunos animales
17+
//       implementarían una interfaz que no utilizarían.
18+
19+
public interface IFlyable
20+
{
21+
void Fly();
22+
}
23+
24+
public interface ISwimmable
25+
{
26+
void Swim();
27+
}
28+
29+
// ____________________________
30+
// Implementar
31+
public class Fish : ISwimmable
32+
{
33+
public void Swim()
34+
{
35+
Console.WriteLine("El pez está nadando.");
36+
}
37+
}
38+
39+
public class Duck : IFlyable, ISwimmable
40+
{
41+
public void Fly()
42+
{
43+
Console.WriteLine("El pato está volando.");
44+
}
45+
46+
public void Swim()
47+
{
48+
Console.WriteLine("El pato está nadando.");
49+
}
50+
}
51+
52+
/*
53+
_______________
54+
* EJERCICIO:
55+
* Crea un gestor de impresoras.
56+
* Requisitos:
57+
* 1. Algunas impresoras sólo imprimen en blanco y negro.
58+
* 2. Otras sólo a color.
59+
* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
60+
* Instrucciones:
61+
* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
62+
* 2. Aplica el ISP a la implementación.
63+
* 3. Desarrolla un código que compruebe que se cumple el principio.
64+
*/
65+
66+
public interface IPrinter
67+
{
68+
void PrintFile(string file);
69+
}
70+
71+
public interface IScanner
72+
{
73+
void ToScan(string pathSave);
74+
}
75+
76+
public interface IFax
77+
{
78+
void SendFile(string file, int phoneNumber);
79+
}
80+
81+
// ____________________________
82+
// Implementar
83+
public class MonoPrinter : IPrinter
84+
{
85+
public void PrintFile(string file)
86+
{
87+
Console.WriteLine("\nImpresora blanco y negro:");
88+
Console.WriteLine(file + " se imprimió.");
89+
}
90+
}
91+
92+
public class ColorPrinter : IPrinter
93+
{
94+
public void PrintFile(string file)
95+
{
96+
Console.WriteLine("\nImpresora a color:");
97+
Console.WriteLine(file + " se imprimió.");
98+
}
99+
}
100+
101+
public class Scanner : IScanner
102+
{
103+
public void ToScan(string pathSave)
104+
{
105+
Console.WriteLine("\nEscaneo realizado, Guardado en: " + pathSave);
106+
}
107+
}
108+
109+
public class Fax : IFax
110+
{
111+
public void SendFile(string file, int phoneNumber)
112+
{
113+
Console.WriteLine("\n-" + file + " Fue enviado a: " + phoneNumber);
114+
}
115+
}
116+
117+
public class MultiFunctionPrinter
118+
{
119+
public MonoPrinter monoPrinter = new();
120+
public ColorPrinter colorPrinter = new();
121+
public Scanner theScanner = new();
122+
public Fax fax = new();
123+
}
124+
125+
//__________________
126+
public class Program
127+
{
128+
static void Main()
129+
{
130+
Fish theFish = new();
131+
Duck theDuck = new();
132+
theFish.Swim();
133+
theDuck.Swim();
134+
theDuck.Fly();
135+
136+
//___________________________
137+
// exs 2
138+
MonoPrinter monoPrinter = new();
139+
monoPrinter.PrintFile("filex.pdf");
140+
141+
ColorPrinter colorPrinter = new();
142+
colorPrinter.PrintFile("filex.pdf");
143+
144+
Scanner theScanner = new ();
145+
theScanner.ToScan("c:\\docs");
146+
147+
Fax fax = new();
148+
fax.SendFile("filex.pdf", 12345678);
149+
150+
Console.WriteLine("\n___________\nMultifunción:");
151+
152+
MultiFunctionPrinter multiFunctionPrinter = new();
153+
multiFunctionPrinter.monoPrinter.PrintFile("filex.pdf");
154+
multiFunctionPrinter.colorPrinter.PrintFile("filex.pdf");
155+
multiFunctionPrinter.theScanner.ToScan("c:\\docs");
156+
multiFunctionPrinter.fax.SendFile("filex.pdf", 12345678);
157+
158+
}
159+
}

0 commit comments

Comments
 (0)