|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * #27 SOLID: PRINCIPIO ABIERTO-CERRADO (OCP) |
| 8 | + * |
| 9 | + * @author martinbohorquez |
| 10 | + */ |
| 11 | +public class martinbohorquez { |
| 12 | + public static void main(String[] args) { |
| 13 | + drawFormaTest(); |
| 14 | + drawFormOCPTest(); |
| 15 | + |
| 16 | + /* |
| 17 | + * DIFICULTAD EXTRA |
| 18 | + */ |
| 19 | + calculatorOCPTest(); |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + private static void drawFormaTest() { |
| 24 | + Forma forma = new Forma(); |
| 25 | + forma.drawSquare(); |
| 26 | + forma.drawCircle(); |
| 27 | + forma.drawTriangle(); |
| 28 | + } |
| 29 | + |
| 30 | + private static void drawFormOCPTest() { |
| 31 | + Form square = new Square(); |
| 32 | + square.draw(); |
| 33 | + Form circle = new Circle(); |
| 34 | + circle.draw(); |
| 35 | + Form triangle = new Triangle(); |
| 36 | + triangle.draw(); |
| 37 | + } |
| 38 | + |
| 39 | + static class Forma { |
| 40 | + private void drawSquare() { |
| 41 | + System.out.println("Dibuja un cuadrado!"); |
| 42 | + } |
| 43 | + |
| 44 | + private void drawCircle() { |
| 45 | + System.out.println("Dibuja un círculo!"); |
| 46 | + } |
| 47 | + |
| 48 | + private void drawTriangle() { |
| 49 | + System.out.println("Dibuja un triángulo!"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + interface Form { |
| 54 | + void draw(); |
| 55 | + } |
| 56 | + |
| 57 | + static class Square implements Form { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void draw() { |
| 61 | + System.out.println("Dibuja un cuadrado!"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static class Circle implements Form { |
| 66 | + |
| 67 | + @Override |
| 68 | + public void draw() { |
| 69 | + System.out.println("Dibuja un círculo!"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static class Triangle implements Form { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void draw() { |
| 77 | + System.out.println("Dibuja un triángulo!"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void calculatorOCPTest() { |
| 82 | + Calculator calculator = new Calculator(); |
| 83 | + calculator.addOperation("addition", new Addition()); |
| 84 | + calculator.addOperation("subtraction", new Subtraction()); |
| 85 | + calculator.addOperation("multiplication", new Multiplication()); |
| 86 | + calculator.addOperation("division", new Division()); |
| 87 | + calculator.addOperation("power", new Power()); |
| 88 | + |
| 89 | + calculator.calculate("addition", 10, 5); |
| 90 | + calculator.calculate("subtraction", 10, 5); |
| 91 | + calculator.calculate("multiplication", 10, 5); |
| 92 | + calculator.calculate("division", 10, 2.5); |
| 93 | + calculator.calculate("power", 5, 3); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + interface Operation { |
| 98 | + double execute(double a, double b); |
| 99 | + } |
| 100 | + |
| 101 | + static class Addition implements Operation { |
| 102 | + |
| 103 | + @Override |
| 104 | + public double execute(double a, double b) { |
| 105 | + return a + b; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static class Subtraction implements Operation { |
| 110 | + |
| 111 | + @Override |
| 112 | + public double execute(double a, double b) { |
| 113 | + return a - b; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + static class Multiplication implements Operation { |
| 118 | + |
| 119 | + @Override |
| 120 | + public double execute(double a, double b) { |
| 121 | + return a * b; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + static class Division implements Operation { |
| 126 | + |
| 127 | + @Override |
| 128 | + public double execute(double a, double b) { |
| 129 | + return a / b; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + static class Power implements Operation { |
| 134 | + |
| 135 | + @Override |
| 136 | + public double execute(double a, double b) { |
| 137 | + return Math.pow(a, b); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + static class Calculator { |
| 142 | + List<Map<String, Operation>> operations; |
| 143 | + |
| 144 | + public Calculator() { |
| 145 | + operations = new ArrayList<>(); |
| 146 | + } |
| 147 | + |
| 148 | + private void addOperation(String name, Operation operation) { |
| 149 | + Map<String, Operation> operationMap = new HashMap<>(); |
| 150 | + operationMap.put(name, operation); |
| 151 | + operations.add(operationMap); |
| 152 | + } |
| 153 | + |
| 154 | + private void calculate(String name, double a, double b) { |
| 155 | + operations.stream() |
| 156 | + .filter(o -> o.containsKey(name)) |
| 157 | + .findFirst() |
| 158 | + .ifPresentOrElse(operation -> |
| 159 | + System.out.printf("%s de %.1f y %.1f: %.2f%n", |
| 160 | + name, a, b, operation.get(name).execute(a, b)), |
| 161 | + () -> System.out.printf("La operación '%s' no está soportada", name)); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments