Strategy Pattern örneği

Run Settings
LanguagePHP
Language Version
Run Command
<?php // Strategy Arayüzü interface PaymentMethodInterface { public function pay($amount); } // Concrete Strategy 1 class CreditCardPayment implements PaymentMethodInterface { public function pay($amount) { echo "Kredi kartı ile ödeme yapıldı: $amount TL"; } } // Concrete Strategy 2 class PayPalPayment implements PaymentMethodInterface { public function pay($amount) { echo "PayPal ile ödeme yapıldı: $amount TL"; } } // Context (Kullanıcı) class Customer { private $paymentMethod; public function setPaymentMethod(PaymentMethodInterface $paymentMethod) { $this->paymentMethod = $paymentMethod; } public function makePayment($amount) { $this->paymentMethod->pay($amount); } } // Kullanım $customer = new Customer(); $customer->setPaymentMethod(new CreditCardPayment()); $customer->makePayment(100); // Kredi kartı ile ödeme yapıldı: 100 TL $customer->setPaymentMethod(new PayPalPayment()); $customer->makePayment(50); // PayPal ile ödeme yapıldı: 50 TL
Editor Settings
Theme
Key bindings
Full width
Lines