|
| 1 | +/* |
| 2 | + El Principio de Segregación de Interfaces (ISP) establece que las implementaciones de una interfaz |
| 3 | + no deben depender de comportamientos que no usen. En otras palabras, una clase no debe implementar métodos que no necesita. |
| 4 | + Por lo cual, es mejor tener muchas interfaces específicas que realicen 1 o 2 tareas que una interfaz general que realice muchas tareas. |
| 5 | + */ |
| 6 | + |
| 7 | +public class Main { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + SimpleBlackAndWhitePrinter bwPrinter = new SimpleBlackAndWhitePrinter(); |
| 11 | + SimpleColorPrinter colorPrinter = new SimpleColorPrinter(); |
| 12 | + MultifunctionPrinter multifunctionPrinter = new MultifunctionPrinter(); |
| 13 | + |
| 14 | + bwPrinter.printBlackAndWhite(); |
| 15 | + colorPrinter.printColor(); |
| 16 | + multifunctionPrinter.printBlackAndWhite(); |
| 17 | + multifunctionPrinter.printColor(); |
| 18 | + multifunctionPrinter.scan(); |
| 19 | + multifunctionPrinter.fax(); |
| 20 | + } |
| 21 | + |
| 22 | + /* |
| 23 | + EJERCICIO: |
| 24 | + Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)" |
| 25 | + y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta. |
| 26 | + */ |
| 27 | + |
| 28 | + // Interfaz general que viola el ISP |
| 29 | + public interface Worker { |
| 30 | + void work(); |
| 31 | + void eat(); |
| 32 | + } |
| 33 | + |
| 34 | + // Trabajador que solo trabaja |
| 35 | + public static class RobotWorker implements Worker { |
| 36 | + @Override |
| 37 | + public void work() { |
| 38 | + System.out.println("Trabajando..."); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void eat() { |
| 43 | + throw new UnsupportedOperationException("No puede comer"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Ejemplo Correcto (Aplicación del ISP) |
| 48 | + // Interfaz específica para trabajar |
| 49 | + public interface Workable { |
| 50 | + void work(); |
| 51 | + } |
| 52 | + |
| 53 | + // Interfaz específica para comer |
| 54 | + public interface Eatable { |
| 55 | + void eat(); |
| 56 | + } |
| 57 | + |
| 58 | + // Trabajador que solo trabaja |
| 59 | + public static class RobotWorker2 implements Workable { |
| 60 | + @Override |
| 61 | + public void work() { |
| 62 | + System.out.println("Trabajando..."); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Trabajador humano que trabaja y come |
| 67 | + public static class HumanWorker implements Workable, Eatable { |
| 68 | + @Override |
| 69 | + public void work() { |
| 70 | + System.out.println("Trabajando..."); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void eat() { |
| 75 | + System.out.println("Comiendo..."); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + DIFICULTAD EXTRA (opcional): |
| 81 | + Crea un gestor de impresoras. |
| 82 | + Requisitos: |
| 83 | + 1. Algunas impresoras sólo imprimen en blanco y negro. |
| 84 | + 2. Otras sólo a color. |
| 85 | + 3. Otras son multifunción, pueden imprimir, escanear y enviar fax. |
| 86 | + Instrucciones: |
| 87 | + 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones. |
| 88 | + 2. Aplica el ISP a la implementación. |
| 89 | + 3. Desarrolla un código que compruebe que se cumple el principio. |
| 90 | + */ |
| 91 | + |
| 92 | + interface BlackAndWhitePrinter { |
| 93 | + void printBlackAndWhite(); |
| 94 | + } |
| 95 | + |
| 96 | + interface ColorPrinter { |
| 97 | + void printColor(); |
| 98 | + } |
| 99 | + |
| 100 | + interface Scanner { |
| 101 | + void scan(); |
| 102 | + } |
| 103 | + |
| 104 | + interface Fax { |
| 105 | + void fax(); |
| 106 | + } |
| 107 | + |
| 108 | + static class SimpleBlackAndWhitePrinter implements BlackAndWhitePrinter { |
| 109 | + @Override |
| 110 | + public void printBlackAndWhite() { |
| 111 | + System.out.println("Printing in black and white..."); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + static class SimpleColorPrinter implements ColorPrinter { |
| 116 | + @Override |
| 117 | + public void printColor() { |
| 118 | + System.out.println("Printing in color..."); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + static class MultifunctionPrinter implements BlackAndWhitePrinter, ColorPrinter, Scanner, Fax { |
| 123 | + |
| 124 | + @Override |
| 125 | + public void printBlackAndWhite() { |
| 126 | + System.out.println("Printing in black and white..."); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void printColor() { |
| 131 | + System.out.println("Printing in color..."); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void fax() { |
| 136 | + System.out.println("Sending fax..."); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void scan() { |
| 141 | + System.out.println("Scanning document..."); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments