1
+ /**
2
+ * #29 SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP)
3
+ *
4
+ * @author martinbohorquez
5
+ */
6
+ public class martinbohorquez {
7
+ public static void main (String [] args ) {
8
+ incorrectISP ();
9
+ correctISP ();
10
+ /*
11
+ * DIFICULTAD EXTRA
12
+ */
13
+ testPrinter ();
14
+
15
+ }
16
+
17
+ private static void incorrectISP () {
18
+ Worker humano = new Humano ();
19
+ humano .work ();
20
+ humano .eat ();
21
+ Worker maquina = new Machine ();
22
+ maquina .work ();
23
+ maquina .eat ();//incorrecto: no se implementa el método "eat()", pero se obliga a definir.
24
+ }
25
+
26
+ private static void correctISP () {
27
+ Human human = new Human ();
28
+ human .work ();
29
+ human .eat ();
30
+ Robot robot = new Robot ();
31
+ robot .work ();
32
+ // robot.eat();//correcto: no se implementa ni existe el método "eat()" para Robot.
33
+ }
34
+
35
+ private static void testPrinter () {
36
+ Printer printer = new PrinterImpl ();
37
+ String document = "doc.pdf" ;
38
+ printer .print (document );
39
+ ColorPrinter colorPrinter = new ColorPrinterImpl ();
40
+ colorPrinter .printColor (document );
41
+ Multifunction multifunctionPrinter = new Multifunction ();
42
+ multifunctionPrinter .print (document );
43
+ multifunctionPrinter .printColor (document );
44
+ multifunctionPrinter .scan (document );
45
+ multifunctionPrinter .sendFax (document );
46
+ }
47
+ }
48
+
49
+ interface Worker {
50
+ void work ();
51
+
52
+ void eat ();
53
+ }
54
+
55
+ class Humano implements Worker {
56
+ @ Override
57
+ public void work () {
58
+ System .out .println ("Trabajando!" );
59
+ }
60
+
61
+ @ Override
62
+ public void eat () {
63
+ System .out .println ("Comiendo!" );
64
+ }
65
+ }
66
+
67
+ class Machine implements Worker {
68
+ @ Override
69
+ public void work () {
70
+ System .out .println ("Trabajando!" );
71
+ }
72
+
73
+ @ Override
74
+ public void eat () {
75
+ // Robot no comen
76
+ }
77
+ }
78
+
79
+ interface Work {
80
+ void work ();
81
+ }
82
+
83
+ interface Eat {
84
+ void eat ();
85
+ }
86
+
87
+ class Human implements Work , Eat {
88
+ @ Override
89
+ public void work () {
90
+ System .out .println ("Trabajando!" );
91
+ }
92
+
93
+ @ Override
94
+ public void eat () {
95
+ System .out .println ("Comiendo!" );
96
+ }
97
+ }
98
+
99
+ class Robot implements Work {
100
+ @ Override
101
+ public void work () {
102
+ System .out .println ("Trabajando!" );
103
+ }
104
+ }
105
+
106
+ interface Printer {
107
+ void print (String document );
108
+ }
109
+
110
+ interface ColorPrinter {
111
+ void printColor (String document );
112
+ }
113
+
114
+ interface Scanner {
115
+ String scan (String document );
116
+ }
117
+
118
+ interface Fax {
119
+ void sendFax (String document );
120
+ }
121
+
122
+ class PrinterImpl implements Printer {
123
+ @ Override
124
+ public void print (String document ) {
125
+ System .out .printf ("Imprimiendo en blanco y negro el documento '%s'%n" , document );
126
+ }
127
+ }
128
+
129
+ class ColorPrinterImpl implements ColorPrinter {
130
+ @ Override
131
+ public void printColor (String document ) {
132
+ System .out .printf ("Imprimiendo a color el documento '%s'%n" , document );
133
+ }
134
+ }
135
+
136
+ class Multifunction implements Printer , ColorPrinter , Scanner , Fax {
137
+
138
+ @ Override
139
+ public void print (String document ) {
140
+ System .out .printf ("Imprimiendo en blanco y negro el documento '%s'%n" , document );
141
+ }
142
+
143
+ @ Override
144
+ public void printColor (String document ) {
145
+ System .out .printf ("Imprimiendo a color el documento '%s'%n" , document );
146
+ }
147
+
148
+ @ Override
149
+ public String scan (String document ) {
150
+ System .out .printf ("Escaneando el documento '%s'%n" , document );
151
+ return "Documento " + document + " escaneado." ;
152
+ }
153
+
154
+ @ Override
155
+ public void sendFax (String document ) {
156
+ System .out .printf ("Enviando por fax el documento '%s'%n" , document );
157
+ }
158
+ }
0 commit comments