Code Refactoring 8

Code Refactoring 8In this part of my code refactoring tutorial, I’ll show you how to eliminate duplicate code using the template method pattern. If you want to see another example of the template method pattern, I cover it here as well Template Method Design Pattern.

When objects perform similar steps in the same order, that is a sign that a template method is needed. To use it implement the similar code in a template method and allow subclasses to override the behavior that varies.

The video and the code below cover all that you need to know.

If you like videos like this, it helps to tell Google

Code From the Video

Sandwich.java

// Generalization is used to transform specific code
// into general purpose code. This simplifies, and 
// removes duplicate code

// The Template Method Pattern helps you eliminate 
// duplicate code. When objects perform similar steps
// in the same order implement the similar code in
// a template method and allow subclasses to override
// the behavior that varies.

public abstract class Sandwich {
	
	// This is the template method
	// It is declared final so subclasses
	// can't override it
	
	final void makeSandwich(){
		
		System.out.println("\n------NEW ORDER------\n");
		
		cutBun();
		
		if(customerWantsMeat()){
			
			addMeat();
			
		}
		
		addVegetables();
		
		if(customerWantsCondiments()){
		
			addCondiments();
		
		}
		
		wrapSandwich();
		
	}
	
	// These methods must be overridden by subclasses
	
	abstract void addMeat();
	abstract void addCondiments();
	
	// These methods are used for every sandwich
	
	public void cutBun(){
		
		System.out.println("The Bun is Cut");
		
	}
	
	public void addVegetables(){
		
		System.out.println("Lettuce, onions & Tomatoes Added");
		
	}
	
	public void wrapSandwich(){
		
		System.out.println("The Sandwich is Wrapped");
		
	}
	
	// These are called hooks. They can be overridden
	// Use abstract methods when you want to force the
	// user to override and a hook when it is optional
	
	boolean customerWantsMeat(){ return true; }
	
	boolean customerWantsCondiments(){ return true; }

}

Hamburger.java

public class Hamburger extends Sandwich{
	
	// BAD WAY
	/*
	private boolean customerWantsCondiments = true;
	
	Hamburger(boolean wantsCondiments){
		
		customerWantsCondiments = wantsCondiments;
		
	}
	
	public void makeSandwich(){
		
		cutBun();
		
		// addMeat() will be overridden by subclasses
		
		addMeat();
			
		addVegetables();
		
		// addCondiments() will be overridden by subclasses
		// customerWantsCondiments() should be created as
		// a hook method that executes optional code
		
		if(customerWantsCondiments()){
		
			addCondiments();
		
		}
		
		wrapSandwich();
		
	}
	*/
	
	void addMeat() {
		
		System.out.println("Hamburger Added");
		
	}


	void addCondiments() {
		
		System.out.println("Special Sauce Added");
		
	}

}

VeggieSub.java

public class VeggieSub extends Sandwich{
	
	// BAD WAY
	/*
	private boolean customerWantsCondiments = true;
		
	VeggieSub(boolean wantsCondiments){
			
		customerWantsCondiments = wantsCondiments;
			
	}
		
	public void makeSandwich(){
			
		cutBun();
				
		addVegetables();
			
		if(customerWantsCondiments()){
			
			addCondiments();
			
		}
			
		wrapSandwich();
			
	}
	*/
	
	boolean customerWantsMeat(){ return false; }

	void addMeat() {
		
	}


	void addCondiments() {
		
		System.out.println("Vinegar and Oil Added");
		
	}

}

Cook.java

public class Cook {
	
	public static void main(String[] args){
		
		Sandwich customer1 = new Hamburger();
		
		customer1.makeSandwich();
		
		Sandwich customer2 = new VeggieSub();
		
		customer2.makeSandwich();
		
	}

}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Google+