Guest User

Untitled

a guest
May 22nd, 2018
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2.  
  3. // Bad demo
  4. $paypal = new Paypal();
  5.  
  6. $paymentManager = new PaymentManager($paypal);
  7.  
  8. $paymentManager->process();
  9.  
  10. class PaymentManager
  11. {
  12. protected $paypal;
  13.  
  14. public function __construct(Paypal $paypal)
  15. {
  16. $this->paypal = $paypal;
  17. }
  18.  
  19. public function process()
  20. {
  21. $this->paypal->processPayment();
  22.  
  23. // ...and other payment stuff
  24. }
  25. }
  26.  
  27. class Paypal
  28. {
  29. public function processPayment()
  30. {
  31. // ...process the payment with PayPal
  32. }
  33. }
  34.  
  35. // Good demo
  36. $paypal = new Paypal();
  37. $creditCard = new CreditCard();
  38.  
  39. $paymentManager = new PaymentManager($paypal);
  40.  
  41. $paymentManager->process();
  42.  
  43. class PaymentManager
  44. {
  45. protected $paymentMethod;
  46.  
  47. public function __construct(PaymentMethodInterface $paymentMethod)
  48. {
  49. $this->paymentMethod = $paymentMethod;
  50. }
  51.  
  52. public function process()
  53. {
  54. $this->paymentMethod->processPayment();
  55.  
  56. // ...and other payment stuff
  57. }
  58. }
  59.  
  60. interface PaymentMethodInterface
  61. {
  62. public function processPayment();
  63. }
  64.  
  65. class Paypal implements PaymentMethodInterface
  66. {
  67. public function processPayment()
  68. {
  69. // ...process the payment with PayPal
  70. }
  71. }
  72.  
  73. class CreditCard implements PaymentMethodInterface
  74. {
  75. public function processPayment()
  76. {
  77. // ...process the payment with PayPal
  78. }
  79. }
Add Comment
Please, Sign In to add comment