|
| 1 | +<?php |
| 2 | + |
| 3 | +// Ejemplo de código que viola el principio de inversión de dependencias |
| 4 | + |
| 5 | +class CreditCardPayment { |
| 6 | + public function process($amount) { |
| 7 | + |
| 8 | + echo "Procesando pago de $amount con tarjeta de crédito\n"; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +class PaymentProcessor { |
| 13 | + private $paymentMethod; |
| 14 | + |
| 15 | + public function __construct() { |
| 16 | + $this->paymentMethod = new CreditCardPayment(); |
| 17 | + } |
| 18 | + |
| 19 | + public function processPayment($amount) { |
| 20 | + $this->paymentMethod->process($amount); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Uso |
| 25 | +echo "\n\nEjemplo de mala implementacion\n\n"; |
| 26 | +$paymentProcessor = new PaymentProcessor(); |
| 27 | +$paymentProcessor->processPayment(100); |
| 28 | + |
| 29 | +?> |
| 30 | + |
| 31 | + |
| 32 | +<?php |
| 33 | + |
| 34 | +// Ejemplo de codigo que cumple el principio de inversion de dependencias |
| 35 | +interface PaymentMethodInterface { |
| 36 | + public function process($amount); |
| 37 | +} |
| 38 | + |
| 39 | +// Implementación concreta de la interfaz |
| 40 | +class CreditCardPayment2 implements PaymentMethodInterface { |
| 41 | + public function process($amount) { |
| 42 | + |
| 43 | + echo "Procesando pago de $amount con tarjeta de crédito\n"; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class PayPalPayment implements PaymentMethodInterface { |
| 48 | + public function process($amount) { |
| 49 | + // Lógica para procesar el pago con PayPal |
| 50 | + echo "Procesando pago de $amount con PayPal\n"; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +class PaymentProcessor2 { |
| 55 | + private $paymentMethod; |
| 56 | + |
| 57 | + public function __construct(PaymentMethodInterface $paymentMethod) { |
| 58 | + $this->paymentMethod = $paymentMethod; |
| 59 | + } |
| 60 | + |
| 61 | + public function processPayment($amount) { |
| 62 | + $this->paymentMethod->process($amount); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +echo "\n\nEjemplo correcto\n\n"; |
| 67 | +$creditCardPayment = new CreditCardPayment2(); |
| 68 | +$paymentProcessor = new PaymentProcessor2($creditCardPayment); |
| 69 | +$paymentProcessor->processPayment(100); |
| 70 | + |
| 71 | +echo "\n\nAhora probemos usando paypal\n\n"; |
| 72 | +$paypalPayment = new PayPalPayment(); |
| 73 | +$paymentProcessor = new PaymentProcessor2($paypalPayment); |
| 74 | +$paymentProcessor->processPayment(200); |
| 75 | + |
| 76 | +// Ejercicio extra |
| 77 | + |
| 78 | +interface NotificationInterface { |
| 79 | + public function send($message, $recipient); |
| 80 | +} |
| 81 | + |
| 82 | +class EmailNotification implements NotificationInterface { |
| 83 | + public function send($message, $recipient) { |
| 84 | + // Lógica para enviar un correo electrónico |
| 85 | + echo "Enviando email a $recipient: $message\n"; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class PushNotification implements NotificationInterface { |
| 90 | + public function send($message, $recipient) { |
| 91 | + // Lógica para enviar una notificación push |
| 92 | + echo "Enviando notificación push a $recipient: $message\n"; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class SMSNotification implements NotificationInterface { |
| 97 | + public function send($message, $recipient) { |
| 98 | + // Lógica para enviar un SMS |
| 99 | + echo "Enviando SMS a $recipient: $message\n"; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class NotificationService { |
| 104 | + private $notificationMethod; |
| 105 | + |
| 106 | + // Inyección de la dependencia a través del constructor |
| 107 | + public function __construct(NotificationInterface $notificationMethod) { |
| 108 | + $this->notificationMethod = $notificationMethod; |
| 109 | + } |
| 110 | + |
| 111 | + public function notify($message, $recipient) { |
| 112 | + $this->notificationMethod->send($message, $recipient); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +echo "\n\nEjercicio extra\n\n"; |
| 117 | + |
| 118 | +$emailNotification = new EmailNotification(); |
| 119 | +$pushNotification = new PushNotification(); |
| 120 | +$smsNotification = new SMSNotification(); |
| 121 | + |
| 122 | +// Crear el servicio de notificaciones con diferentes métodos |
| 123 | +$emailService = new NotificationService($emailNotification); |
| 124 | +$pushService = new NotificationService($pushNotification); |
| 125 | +$smsService = new NotificationService($smsNotification); |
| 126 | + |
| 127 | +// Enviar notificaciones utilizando el servicio |
| 128 | +$emailService-> notify( "Hola, mundo!", "[email protected]"); |
| 129 | +$pushService->notify("Tienes una nueva actualización", "usuario123"); |
| 130 | +$smsService->notify("Tu código de verificación es 123456", "666666666"); |
0 commit comments