Design Patterns-Template method patterns (template methods pattern) detailed

Source: Internet
Author: User
Tags readline stub
Template Method Mode (template methods pattern) detailed


This article address: Http://blog.csdn.net/caroline_wendy


template Approach Pattern: Defines the skeleton of an algorithm in a method, and delays some steps into subclasses. \ template

Template methods allow subclasses to redefine certain steps in an algorithm without changing the structure of the algorithm.


Template methods can be hooks (hooks), a method that is declared in an abstract class, but only empty or default implementations.

The presence of hooks allows subclasses to have the ability to hook up different points of the algorithm.


Framework for abstract classes:

/**
 * @time June 18, 2014 * * *
package template_method;

/**
 * @author C.l.wang
 * * *
 /Public
abstract class AbstractClass {
	
	final void Templatemethod () {
		PrimitiveOperation1 ();
		PrimitiveOperation2 ();
		Concreteoperation ();
		Hook ();
	}
	
	abstract void PrimitiveOperation1 ();
	
	abstract void PrimitiveOperation2 ();
	
	final void Concreteoperation () {
		
	}
	
	void Hook () {}
}


Object-oriented principles:

Hollywood rule: Don't call us, we'll call you.


Specific methods:

1. Abstraction Class (abstract Class), containing template methods (template method), Abstract Operations (Abstracts operation),

specific operations (concrete operation), and hooks (hook).

/**
 * @time June 18, 2014 * * *
package template_method;

/**
 * @author C.l.wang
 * * *
 /Public
abstract class Caffeinebeverage {
	
	final void Preparerecipe () {// Template Method
		Boilwater ();
		Brew ();
		Pourincup ();
		if (customerwantscondiments ()) {
			addcondiments ();
		}
	}
	
	abstract void Brew (); Abstract
	
	void addcondiments ();
	
	void Boilwater () {//specific operation
		System.out.println ("boiling water");
	}
	
	void Pourincup () {
		System.out.println ("pouring into Cup");
	}
	
	Boolean customerwantscondiments () {//Hook return
		true;
	}
	
}

2. Specific Class (concrete Class), Inheritance (extend) abstract class (abstract class).

/** * @time June 18, 2014 * * * Package template_method;
Import Java.io.BufferedReader;
Import java.io.IOException;

Import Java.io.InputStreamReader; /** * @author C.l.wang * */public class Coffeewithhook extends Caffeinebeverage {/* (non-javadoc) * @see template _method.  Caffeinebeverage#brew () */@Override void Brew () {//TODO auto-generated Method stub System.out.println ("Dripping
	Coffee through filter "); }/* (non-javadoc) * @see Template_method. Caffeinebeverage#addcondiments () */@Override void addcondiments () {//TODO auto-generated method stub System.out
	. println ("Adding Sugar and Milk");
		
		public Boolean customerwantscondiments () {//Hook String answer = Getuserinput ();
		if (Answer.tolowercase (). StartsWith ("Y")) {return true;
		else {return false;
		
		} private String Getuserinput () {string answer = NULL;
		
		System.out.println ("Would You like milk and sugar with your coffee (y/n)"); BufferedReader in = new BufFeredreader (New InputStreamReader (system.in));
		try {answer = In.readline ();
		catch (IOException IoE) {System.out.println ("IO error trying to read your answer");
		} if (answer = null) {return "no";
	return answer;

}/** * @time June 18, 2014 * * * Package template_method;
Import Java.io.BufferedReader;
Import java.io.IOException;

Import Java.io.InputStreamReader; /** * @author C.l.wang * */public class Teawithhook extends Caffeinebeverage {/* (non-javadoc) * @see Template_me Thod.  Caffeinebeverage#brew () */@Override void Brew () {//TODO auto-generated Method Stub System.out.println ("Steeping
	The tea "); }/* (non-javadoc) * @see Template_method. Caffeinebeverage#addcondiments () */@Override void addcondiments () {//TODO auto-generated method stub System.out
	. println ("adding Lemon");
		
		public Boolean customerwantscondiments () {String answer = getuserinput (); if (Answer.tolowercase (). StartsWith ("Y")) {RETurn true;
		else {return false;
		
		} private String Getuserinput () {string answer = NULL;
		
		System.out.println ("Would you like lemon with your tea (y/n)");
		
		BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
		try {answer = In.readline ();
		catch (IOException IoE) {System.out.println ("IO error trying to read your answer");
		} if (answer = null) {return "no";
	return answer;
 }
	
}

3. Test ClassContains Hooks (Hook) Operation.

/**
 * @time June 18, 2014 * * *
package template_method;

/**
 * @author C.l.wang * */Public
class Beveragetestdrive {

	/**
	 * @param args
	 * * Public
	static void Main (string[] args) {
		//TODO auto-generated method stub
		Teawithhook teahook = new TEAW Ithhook ();
		Coffeewithhook Coffeehook = new Coffeewithhook ();
		
		System.out.println ("\nmaking tea ...");
		Teahook.preparerecipe ();
		
		System.out.println ("\nmaking coffee ...");
		Coffeehook.preparerecipe ();
	}



4. Output:

Making tea ...
Boiling water
steeping the tea
pouring into cup Would your like lemon and
your tea (y/n)? 
Y
adding Lemon

making coffee ...
Boiling water
dripping Coffee through filter
pouring into Cup
Would your like milk and sugar with your Coffee (y/n)? 
N









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.