Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /*
- * Template method Pattern
- *
- * Use the same algorithm and remove the duplication by creating abstract methods for everything that's unique and can change from class to class.
- * Used in laravel for authentication with multiple account providers like twitter, github or facebook.
- */
- abstract class Sub{
- public function make()
- {
- return $this
- ->layBread()
- ->addLettuce()
- ->addPrimaryToppings()
- ->addSauces();
- }
- protected function layBread()
- {
- var_dump('laying down the bread');
- return $this;
- }
- protected function addLettuce()
- {
- var_dump('add some lettuce');
- return $this;
- }
- protected function addSauces()
- {
- var_dump('add oil and vinegar');
- return $this;
- }
- protected abstract function addPrimaryToppings();
- }
- class TurkeySub extends Sub{
- public function addPrimaryToppings()
- {
- var_dump('add some turkey');
- return $this;
- }
- }
- class HamSub extends Sub{
- public function addPrimaryToppings()
- {
- var_dump('add some ham');
- return $this;
- }
- }
- class VeggieSub extends Sub{
- public function addPrimaryToppings()
- {
- var_dump('add some veggies');
- return $this;
- }
- }
- //testing
- (new HamSub)->make();
- (new VeggieSub)->make();
- (new TurkeySub)->make();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement