|
| 1 | +' ╔══════════════════════════════════════╗ |
| 2 | +' ║ Autor: Kenys Alvarado ║ |
| 3 | +' ║ GitHub: https://github.com/Kenysdev ║ |
| 4 | +' ║ 2024 - VB.NET ║ |
| 5 | +' ╚══════════════════════════════════════╝ |
| 6 | +'----------------------------------------------------- |
| 7 | +'* SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP) |
| 8 | +'----------------------------------------------------- |
| 9 | +' - Una clase no debería estar obligada a implementar interfaces que no utiliza. |
| 10 | +' Evitando crear grandes clases monolíticas. |
| 11 | + |
| 12 | +'__________________________ |
| 13 | +' NOTA: Este ejemplo muestra el uso CORRECTO. Para suponer un ejemplo que viole el principio, sería. |
| 14 | +' Imaginar todos los métodos siguientes, en una sola interfaz, entonces algunos dispositivos |
| 15 | +' implementarían una interfaz que no necesitan. |
| 16 | + |
| 17 | +Public Interface IPlayable |
| 18 | + Sub Play() |
| 19 | +End Interface |
| 20 | + |
| 21 | +Public Interface IDisplayable |
| 22 | + Sub Display() |
| 23 | +End Interface |
| 24 | + |
| 25 | +' ____________________________________________________ |
| 26 | +' Implementar |
| 27 | +Public Class Speaker |
| 28 | + Implements IPlayable |
| 29 | + |
| 30 | + Public Sub Play() Implements IPlayable.Play |
| 31 | + Console.WriteLine("El altavoz está reproduciendo música.") |
| 32 | + End Sub |
| 33 | +End Class |
| 34 | + |
| 35 | +Public Class Phone |
| 36 | + Implements IPlayable, IDisplayable |
| 37 | + |
| 38 | + Public Sub Play() Implements IPlayable.Play |
| 39 | + Console.WriteLine("El teléfono está reproduciendo una canción.") |
| 40 | + End Sub |
| 41 | + |
| 42 | + Public Sub Display() Implements IDisplayable.Display |
| 43 | + Console.WriteLine("El teléfono está mostrando la pantalla de reproducción.") |
| 44 | + End Sub |
| 45 | +End Class |
| 46 | + |
| 47 | +'__________________________ |
| 48 | +'* EJERCICIO |
| 49 | +'* Crea un gestor de impresoras. |
| 50 | +'* Requisitos: |
| 51 | +'* 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 52 | +'* 2. Otras sólo a color. |
| 53 | +'* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 54 | +'* Instrucciones: |
| 55 | +'* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 56 | +'* 2. Aplica el ISP a la implementación. |
| 57 | +'* 3. Desarrolla un código que compruebe que se cumple el principio. |
| 58 | + |
| 59 | +Public Interface IPrinter |
| 60 | + Sub PrintFile(file As String) |
| 61 | +End Interface |
| 62 | + |
| 63 | +Public Interface IScanner |
| 64 | + Sub ToScan(pathSave As String) |
| 65 | +End Interface |
| 66 | + |
| 67 | +Public Interface IFax |
| 68 | + Sub SendFile(file As String, phoneNumber As Integer) |
| 69 | +End Interface |
| 70 | + |
| 71 | +' ____________________________________________________ |
| 72 | +' Implementaciones |
| 73 | +Public Class MonoPrinter |
| 74 | + Implements IPrinter |
| 75 | + |
| 76 | + Public Sub PrintFile(file As String) Implements IPrinter.PrintFile |
| 77 | + Console.WriteLine(vbCrLf + "Impresora blanco y negro:") |
| 78 | + Console.WriteLine(file + " se imprimió.") |
| 79 | + End Sub |
| 80 | +End Class |
| 81 | + |
| 82 | +Public Class ColorPrinter |
| 83 | + Implements IPrinter |
| 84 | + |
| 85 | + Public Sub PrintFile(file As String) Implements IPrinter.PrintFile |
| 86 | + Console.WriteLine(vbCrLf + "Impresora a color:") |
| 87 | + Console.WriteLine(file + " se imprimió.") |
| 88 | + End Sub |
| 89 | +End Class |
| 90 | + |
| 91 | +Public Class Scanner |
| 92 | + Implements IScanner |
| 93 | + |
| 94 | + Public Sub ToScan(pathSave As String) Implements IScanner.ToScan |
| 95 | + Console.WriteLine(vbCrLf + "Escaneo realizado, Guardado en: " + pathSave) |
| 96 | + End Sub |
| 97 | +End Class |
| 98 | + |
| 99 | +Public Class Fax |
| 100 | + Implements IFax |
| 101 | + |
| 102 | + Public Sub SendFile(file As String, phoneNumber As Integer) Implements IFax.SendFile |
| 103 | + Console.WriteLine(vbCrLf + $"{file} Fue enviado a: {phoneNumber}") |
| 104 | + End Sub |
| 105 | +End Class |
| 106 | + |
| 107 | +Public Class MultiFunctionPrinter |
| 108 | + Public monoPrinter As New MonoPrinter |
| 109 | + Public colorPrinter As New ColorPrinter |
| 110 | + Public theScanner As New Scanner |
| 111 | + Public fax As New Fax |
| 112 | +End Class |
| 113 | + |
| 114 | +' ____________________________________________________ |
| 115 | +Public Module Program |
| 116 | + Public Sub Main() |
| 117 | + ' Exs 1 |
| 118 | + Dim phone As New Phone() |
| 119 | + phone.Play() |
| 120 | + phone.Display() |
| 121 | + |
| 122 | + Dim speaker As New Speaker() |
| 123 | + speaker.Play() |
| 124 | + |
| 125 | + '_____________________________________________ |
| 126 | + ' Exs 2 |
| 127 | + Console.WriteLine(vbCrLf + "Exs #2") |
| 128 | + |
| 129 | + Dim MonoPrinter As New MonoPrinter |
| 130 | + MonoPrinter.PrintFile("filex.pdf") |
| 131 | + |
| 132 | + Dim ColorPrinter As New ColorPrinter |
| 133 | + ColorPrinter.PrintFile("filex.pdf") |
| 134 | + |
| 135 | + Dim theScanner As New Scanner |
| 136 | + theScanner.ToScan("c:\\docs") |
| 137 | + |
| 138 | + Dim Fax As New Fax |
| 139 | + Fax.SendFile("filex.pdf", 12345678) |
| 140 | + |
| 141 | + Console.WriteLine(vbCrLf + "___________" + vbCrLf + "Multifunción") |
| 142 | + |
| 143 | + Dim MultiFunctionPrinter As New MultiFunctionPrinter |
| 144 | + MultiFunctionPrinter.monoPrinter.PrintFile("filex.pdf") |
| 145 | + MultiFunctionPrinter.colorPrinter.PrintFile("filex.pdf") |
| 146 | + MultiFunctionPrinter.theScanner.ToScan("c:\\docs") |
| 147 | + MultiFunctionPrinter.fax.SendFile("filex.pdf", 12345678) |
| 148 | + |
| 149 | + End Sub |
| 150 | +End Module |
0 commit comments