Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. <?
  2. /*
  3. * Template method Pattern
  4. *
  5. * Use the same algorithm and remove the duplication by creating abstract methods for everything that's unique and can change from class to class.
  6. * Used in laravel for authentication with multiple account providers like twitter, github or facebook.
  7. */
  8.  
  9.  
  10. abstract class Sub{
  11. public function make()
  12. {
  13. return $this
  14. ->layBread()
  15. ->addLettuce()
  16. ->addPrimaryToppings()
  17. ->addSauces();
  18. }
  19.  
  20. protected function layBread()
  21. {
  22. var_dump('laying down the bread');
  23. return $this;
  24. }
  25.  
  26. protected function addLettuce()
  27. {
  28. var_dump('add some lettuce');
  29. return $this;
  30. }
  31.  
  32. protected function addSauces()
  33. {
  34. var_dump('add oil and vinegar');
  35. return $this;
  36. }
  37.  
  38. protected abstract function addPrimaryToppings();
  39. }
  40.  
  41. class TurkeySub extends Sub{
  42. public function addPrimaryToppings()
  43. {
  44. var_dump('add some turkey');
  45. return $this;
  46. }
  47. }
  48.  
  49. class HamSub extends Sub{
  50. public function addPrimaryToppings()
  51. {
  52. var_dump('add some ham');
  53. return $this;
  54. }
  55. }
  56.  
  57. class VeggieSub extends Sub{
  58. public function addPrimaryToppings()
  59. {
  60. var_dump('add some veggies');
  61. return $this;
  62. }
  63. }
  64.  
  65. //testing
  66. (new HamSub)->make();
  67. (new VeggieSub)->make();
  68. (new TurkeySub)->make();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement