Template Method Design Pattern

[ad_1]
Get the code here:

Welcome to my Template Method Design Pattern Tutorial. It is an extremely easy design pattern to understand and use.

With this pattern, you define a method (algorithm) in an abstract class. It contains both abstract methods and non-abstract methods. The subclasses that extend this abstract class then override those methods that don’t make sense for them to use in the default way.

It may sound complicated, but it definitely isn’t. It is also extremely useful. Look at the code above to get a complete understanding quickly.


Posted

in

by

Tags:

Comments

20 responses to “Template Method Design Pattern”

  1. Edward Ryklin Avatar

    I would make one change… do not declare addMeat() as abstract. Instead implement in the base class as : addMeat() { } like that, along with addCheese() etc. By doing this, those subclasses like VeggieHoagie do not need to implement any of these functions and only those subclasses that do use these function can override them.

  2. Omair Bhore Avatar

    Whoa! Are you from Philly ?

  3. jeremy forrest Avatar

    Great tutorial. You explain very well even though you move fast. Subscribed!

  4. Tibi Mose Avatar

    My issue with this is the the hook (boolean) methods are dependent of the abstract methods and you have to look anyway in the superclass what is the implementation of makeSandwich(). I don't see how this pattern can be useful, unless you can force the implementation of addMean() only if checkWantsMeat() returns true and I don't see a way to do that.

  5. Nincako Nincako Avatar

    since some classes may not need some methods inherited by abstract class like add meat why do you make it abstract?
    I have the same concern asked but not replied
    "I don't like having empty method implementations, is there any way around it with this pattern????"

  6. Bharani Krishna Avatar

    Your content is amazing. I appreciate all the effort you put into these videos. Keep up the good work man. 🙂

  7. SuperDranger Avatar

    Thank you so much please please keep up the good work.

  8. Pushkaraj Patil Avatar

    Hello Derek,
    Thank you for this video…

    In video, I can see that, in one of "if" condition (customerWantsCondiments, in Hoagie class, makeSandwich() function) you are calling addVegetables(), still you are getting right output… how?????

  9. xSkuulz Avatar

    Could you do a concrete sample project implementing all those design patterns ? It seems like they resolve very specific problems and I have a hard time seeing how it could refactor that much code.

  10. Kri38 Avatar

    you are good man

  11. Bharat Bhasin Avatar

    Should we us so many if statements? Is there different way?

  12. AnonymousAngelo Avatar

    This was awesome, thank you 🙂

  13. Venkata sharma Y N B Avatar

    really great place to go through all patterns, I have one suggestion. it would be better if you can explain each example how it would have been without using specif pattern or at-least talk about them once after finished with example.

  14. Lisandro Chichi Penzo Avatar

    I hate when you zoom in and suddenly zoom out, great tutorial

  15. Jochem De Kruijf Avatar

    Epic set of tutorials <3

  16. Tanuj Nayanam Avatar

    Hello, Since we over rode and made the addMeat() an empty method. So anyways addMeat() will not do anything now in the VeggieHoagie. Then what is the benefit of having hooks and making it to false?

  17. Derek Baker Avatar

    Thanks for these videos! I just want to say you forgot to change the 'customerWantsCondiments' implementation on Hoagie.java (min 6:38). It says "addVegetables()" and should be "addCondiments()" (may be you can just add a corrective note there). But that's just a minimum common error when copying and pasting 😛
    Thank you very much!

  18. danobot Avatar

    this tutorial made me really hungry. That sandwich sounds awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *