Template Method Design Pattern Tutorial

Template Method Design Pattern TutorialWelcome 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 after the video to get a complete understanding quickly.

If you like videos like this, tell Google by clicking this

Share if you think someone would like this

Code from the Video

ITALIANHOAGIE.JAVA

public class ItalianHoagie extends Hoagie{
	
	String[] meatUsed = { "Salami", "Pepperoni", "Capicola Ham" };
	String[] cheeseUsed = { "Provolone" };
	String[] veggiesUsed = { "Lettuce", "Tomatoes", "Onions", "Sweet Peppers" };
	String[] condimentsUsed = { "Oil", "Vinegar" };
	
	public void addMeat(){
		
		System.out.print("Adding the Meat: ");
		
		for (String meat : meatUsed){
			
			System.out.print(meat + " ");
			
		}
		
	}
	
	public void addCheese(){
		
		System.out.print("Adding the Cheese: ");
		
		for (String cheese : cheeseUsed){
			
			System.out.print(cheese + " ");
			
		}
		
	}
	
	public void addVegetables(){
		
		System.out.print("Adding the Vegetables: ");
		
		for (String vegetable : veggiesUsed){
			
			System.out.print(vegetable + " ");
			
		}
		
	}
	
	public void addCondiments(){
		
		System.out.print("Adding the Condiments: ");
		
		for (String condiment : condimentsUsed){
			
			System.out.print(condiment + " ");
			
		}
		
	}
	
}
	
	/*
	public void makeSandwich(){
		
		cutBun();
		addMeat();
		addCheese();
		addVegetables();
		addCondiments();
		wrapTheHoagie();
		
	}
	
	public void cutBun(){
		
		System.out.println("The Hoagie is Cut");
		
	}
	
	public void addMeat(){
		
		System.out.println("Add Salami, Pepperoni and Capicola ham");
		
	}
	
	public void addCheese(){
		
		System.out.println("Add Provolone");
		
	}
	
	public void addVegetables(){
		
		System.out.println("Add Lettuce, Tomatoes, Onions and Sweet Peppers");
		
	}
	
	public void addCondiments(){
		
		System.out.println("Add Oil and Vinegar");
		
	}
	
	public void wrapTheHoagie(){
		
		System.out.println("Wrap the Hoagie");
		
	}
	
}
	*/

HOAGIE.JAVA

// A Template Method Pattern contains a method that provides
// the steps of the algorithm. It allows subclasses to override 
// some of the methods

public abstract class Hoagie {
	
	boolean afterFirstCondiment = false;
	
	// This is the Template Method
	// Declare this method final to keep subclasses from
	// changing the algorithm
	
	final void makeSandwich(){
		
		cutBun();
		
		if(customerWantsMeat()){
			
			addMeat();
			
			// Here to handle new lines for spacing
			afterFirstCondiment = true;
			
		}
		
		if(customerWantsCheese()){
			
			if(afterFirstCondiment) { System.out.print("\n"); }
			
			addCheese();
			
			afterFirstCondiment = true;
			
		}
		
		if(customerWantsVegetables()){
			
			if(afterFirstCondiment) { System.out.print("\n"); }
			
			addVegetables();
			
			afterFirstCondiment = true;
			
		}
		
		if(customerWantsCondiments()){
			
			if(afterFirstCondiment) { System.out.print("\n"); }
			
			addCondiments();
			
			afterFirstCondiment = true;
			
		}
		
		wrapTheHoagie();
		
	}
	
	// These methods must be overridden by the extending subclasses
	
	abstract void addMeat();
	abstract void addCheese();
	abstract void addVegetables();
	abstract void addCondiments();
	
	public void cutBun(){
		
		System.out.println("The Hoagie is Cut");
		
	}
	
	// These are called hooks
	// If the user wants to override these they can
	
	// Use abstract methods when you want to force the user
	// to override and use a hook when you want it to be optional
	
	boolean customerWantsMeat() { return true; }
	boolean customerWantsCheese() { return true; }
	boolean customerWantsVegetables() { return true; }
	boolean customerWantsCondiments() { return true; }
	
	public void wrapTheHoagie(){
		
		System.out.println("\nWrap the Hoagie");
		
	}
	
	public void afterFirstCondiment(){
		
		System.out.println("\n");
		
	}
	
}

VEGGIEHOAGIE.JAVA

public class VeggieHoagie extends Hoagie{

	String[] veggiesUsed = { "Lettuce", "Tomatoes", "Onions", "Sweet Peppers" };
	String[] condimentsUsed = { "Oil", "Vinegar" };
	
	boolean customerWantsMeat() { return false; }
	boolean customerWantsCheese() { return false; }
	
	public void addVegetables(){
		
		System.out.print("Adding the Vegetables: ");
		
		for (String vegetable : veggiesUsed){
			
			System.out.print(vegetable + " ");
			
		}
		
	}
	
	public void addCondiments(){
		
		System.out.print("Adding the Condiments: ");
		
		for (String condiment : condimentsUsed){
			
			System.out.print(condiment + " ");
			
		}
		
	}

	void addMeat() {}
	
	void addCheese() {}
	
	
}

SANDWICHSCULPTOR.JAVA

public class SandwichSculptor {
	
	public static void main(String[] args){
		
		ItalianHoagie cust12Hoagie = new ItalianHoagie();
		
		cust12Hoagie.makeSandwich();
		
		System.out.println();
		
		VeggieHoagie cust13Hoagie = new VeggieHoagie();
		
		cust13Hoagie.makeSandwich();
		
	}
	
}

6 Responses to “Template Method Design Pattern Tutorial”

  1. Andrew says:

    Please can you make a video series dedicated to Java FX 2.2 ?
    EX: How to create an program that stores phone models(every model with their picture) inside a database. And pack the app inside a jar or installer for distribution.

    • admin says:

      Thank you for the request and I’ll definitely cover Java FX. By the end of this series I will cover pretty much everything there is to know about java and good programming

  2. humanshu says:

    excellent work..keep it up..please include if possible UML diagrams also..:)

  3. Alberto Romero says:

    Hey Derek!
    I’m not getting tired of seeing your videos, I’m really learning a lot!! I’m actually studying computer sciences and this is helping me like a lot! If there’s anything i could do for you please let me know! You’re simply incredible, thanks for your videos 🙂

Leave a Reply

Your email address will not be published.

Google+