Head First design mode study notes-template method mode

Source: Internet
Author: User

The template method mode is the behavior mode of the class. Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. This is the intention of the template method mode.
Design ModeTemplate Method mode: defines an algorithm framework in a method, and delays some steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm results. A template is a method that defines an algorithm as a set of steps. Any steps can be abstract and implemented by sub-classes. This ensures that the algorithm structure remains unchanged and is partially implemented by sub-classes.
A hook is a method declared in an abstract class, but it only has null or default implementation. The existence of a hook allows the subclass to hook different points of the algorithm. Do you want to hook it, sub-classes are independent. The real purpose of using hooks: hooks allow subclasses to implement the optional part of the algorithm, or subclass can ignore this hook when the implementation of the hook is not important to the subclass. Another usage of hooks is to give subclass the opportunity to respond to some of the upcoming (or just-occurring) steps in the template method.
Design principlesHollywood principle: Don't call us. We will call you. Under Hollywood principles, we allow lower-layer components to hook ourselves to the system, but higher-level components decide when and how to use these lower-layer components. In other words, the method for high-level components to operate on is "Don't call us, we will call you ".
Key PointsThe "template method" defines the algorithm steps and delays implementation of these steps to the subclass. The template method mode provides an important technique for code reuse. Abstract classes of template methods can define specific methods, abstract methods, and hooks. To prevent the subclass from modifying the algorithm in the template method, we can declare the template method as final. Both policy mode and template method mode are encapsulation algorithms. The former uses a combination, and the latter uses inheritance. The factory method is a special version of the template method.
Application of typical board Detection Methods1. HttpServlet Technology 2. Swing window Program 3. Applet
Template Method mode:

// Declare it as an abstract class. The subclass must perform its public abstract class CaffeineBeverage {// declare it as final. You do not want the subclass to overwrite final void prepareRecipe () {boilWater (); brew (); pourInCup (); addCondiments () ;}// declared as an abstract class. The subclass must perform the following operations: abstract void brew (); abstract void addCondiments (); void boilWater () {System. out. println ("Boiling water");} void pourInCup () {System. out. println ("Pouring into cup") ;}// extends inherits from public class Tea extends CaffeineBeverage {// The abstract method public void brew () {System. out. println ("Steeping the tea");} public void addCondiments () {System. out. println ("Adding Lemon ");}}

HOOK:
Public abstract class CaffeineBeverageWithHook {void prepareRecipe () {boilWater (); brew (); pourInCup (); if (mermerwantscondiments () {addCondiments ();}} abstract void brew (); abstract void addCondiments (); void boilWater () {System. out. println ("Boiling water");} void pourInCup () {System. out. println ("Pouring into cup");} // defines a default implementation, which can be overwritten by the subclass, but boolean mermerwantscondiments () {return true;} p Ublic class CoffeeWithHook extends CaffeineBeverageWithHook {public void brew () {System. out. println ("Dripping Coffee through filter");} public void addCondiments () {System. out. println ("Adding Sugar and Milk");} // overwrites this method and provides its own public boolean customerWantsCondiments () {// Let the user input the decision on the seasoning String answer = getUserInput (); if (answer. toLowerCase (). startsWith ("y") {return true;} else {return false;} priva Te String getUserInput () {String answer = null; System. out. print ("wocould 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. err. println ("IO error trying to read your answer");} if (answer = null) {return "no" ;}return answer ;}}


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.