Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Bad demo
- $paypal = new Paypal();
- $paymentManager = new PaymentManager($paypal);
- $paymentManager->process();
- class PaymentManager
- {
- protected $paypal;
- public function __construct(Paypal $paypal)
- {
- $this->paypal = $paypal;
- }
- public function process()
- {
- $this->paypal->processPayment();
- // ...and other payment stuff
- }
- }
- class Paypal
- {
- public function processPayment()
- {
- // ...process the payment with PayPal
- }
- }
- // Good demo
- $paypal = new Paypal();
- $creditCard = new CreditCard();
- $paymentManager = new PaymentManager($paypal);
- $paymentManager->process();
- class PaymentManager
- {
- protected $paymentMethod;
- public function __construct(PaymentMethodInterface $paymentMethod)
- {
- $this->paymentMethod = $paymentMethod;
- }
- public function process()
- {
- $this->paymentMethod->processPayment();
- // ...and other payment stuff
- }
- }
- interface PaymentMethodInterface
- {
- public function processPayment();
- }
- class Paypal implements PaymentMethodInterface
- {
- public function processPayment()
- {
- // ...process the payment with PayPal
- }
- }
- class CreditCard implements PaymentMethodInterface
- {
- public function processPayment()
- {
- // ...process the payment with PayPal
- }
- }
Add Comment
Please, Sign In to add comment