Template mode in Design Mode

Source: Internet
Author: User

1 Overview

Template Patern)Defines the skeleton of an algorithm in a method, and delays some steps to the subclass. The template method allows the subclass to redefine some steps in the algorithm without changing the algorithm structure.

A template is a method that defines an algorithm as a set of steps. In order not to change the quilt class, this method is usually limited to the final type. There is anotherHook)The so-called "Hook" is the default method that does not do things. This method is not implemented in the abstract class or is empty. The subclass can decide whether to overwrite it. Naturally, this method is called in the "template" method.

When the subclass must provide the implementation of a method in the "template", this method is defined as an abstract method in the parent class. If this method is optional, or when the implementation of sub-classes is not important, you can use hooks to control them. By using hooks, subclass can respond to the upcoming steps in the template method.

2 Examples

For example, there are many examples on the internet, examples of cooking, examples of cooking, examples of coffee and tea, and so on, to illustrate the use of the template mode. I will not talk about those examples here. I will write my own skills.

Currently, there are many Android mobile phone applications, but they must be installed on the mobile phone before they can be used. During the installation, We need to abstract the following steps: apply for the installation permission, create the appropriate directory, register the service, and select whether to start the service. Let's take this example to describe the use of the template mode.

Create an abstract class to describe all installation steps:

1 package org. scott. template; 2/** 3 * @ author Scott 4 * @ date December 25, 2013 5 * @ description 6 */7 public abstract class Setup {8 public final void setup () {9 getAuthority (); 10 createDir (); 11 registerService (); 12 13 if (isAutoStart () {14 autoStart (); 15} 16 17 printMsg (); 18} 19 20 public abstract void getAuthority (); 21 public abstract void createDir (); 22 public abstract void registerService (); 23 24 public boolean isAutoStart () {25 return false; 26}; 27 28 public void printMsg () {29 System. out. println ("The app has set up over. "); 30} 31 32 private void autoStart () {33 System. out. println ("The app will start automatically, has add it to the auto-start list. "); 34} 35}

The setup method here is of the final type, that is, the "template" method we mentioned above. Each algorithm is defined as the installation step, including obtaining permissions, creating directories, registering services, and starting services. Whether or not to start the hook is determined by the subclass.

The following is a specific implementation:

1 package org. scott. template; 2/** 3 * @ author Scott 4 * @ date February 5 * @ description 6 */7 public class YoudaoSetup extends Setup {8 9 @ Override10 public void getAuthority () {11 System. out. println ("Has got the set up authority. "); 12} 13 14 @ Override15 public void createDir () {16 System. out. println ("Create Youdao directory. "); 17} 18 19 @ Override20 public void registerService () {21 System. out. println ("Register the Youdao service. "); 22} 23 24 @ Override25 public boolean isAutoStart () {26 return true; 27} 28 29}

Here, we overwrite the isAutoStart method, set it to true, and add the current software to the auto-start list. Hook, which can be overwritten in the subclass or directly used by default in the parent class. The following QQ installation is the default implementation:

1 package org. scott. template; 2/** 3 * @ author Scott 4 * @ date February 5 * @ description 6 */7 public class QQSetup extends Setup {8 9 @ Override10 public void getAuthority () {11 System. out. println ("Has got the set up authority. "); 12} 13 14 @ Override15 public void createDir () {16 System. out. println ("Create QQ directory. "); 17} 18 19 @ Override20 public void registerService () {21 System. out. println ("Register the QQ service. "); 22} 23}

The above is basically the implementation of the abstract template and specific template. The template method is not actually modified in the subclass, because we set it to the final type, sub-classes modify some specific implementation algorithms in the template method, such as different software. Their installation directories and registered services are different. We use these methods as abstract methods, subclass must implement its own specific method.

Write a test class:

1 package org. scott. template; 2/** 3 * @ author Scott 4 * @ date February 5 * @ description 6 */7 public class TemplateTest {8 9 public static void main (String [] args) {10 Setup qqSetup = new QQSetup (); 11 Setup youdaoSetup = new YoudaoSetup (); 12 13 qqSetup. setup (); 14 System. out. println ("\ n --------------------------- \ n"); 15 youdaoSetup. setup (); 16} 17 18}

Test results:

 1 Has got the set up authority. 2 Create QQ directory. 3 Register the QQ service. 4 The app has set up over. 5  6 --------------------------- 7  8 Has got the set up authority. 9 Create Youdao directory.10 Register the Youdao service.11 The app will start automatically, has add it to the auto-start list.12 The app has set up over.

3 Summary

In the example above, we can see that there are two roles when using the template:

(1) Abstract template role: Defines one or more abstract operations to implement sub-classes. These abstract operations are called basic operations.

(2) specific template roles: Implements one or more abstract methods defined by the parent class.

The template mode mainly implements code reuse and complies with the open-closed principle. Because an abstract class is introduced, if there are too many implementations, the relationship between classes is complex..

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.