Android design mode: Template Method mode; android Design Mode

Source: Internet
Author: User

Android design mode: Template Method mode; android Design Mode

Template Method mode definition: defines an algorithm skeleton in a method, and delays some steps to the subclass. The template method allows the subclass to keep the algorithm structure unchanged, redefines some steps in the algorithm.

Design Principle: Don't call us. I will call you (just like headhunters and job seekers, job seekers don't need to find headhunters, and headhunters will find you if they have jobs ).

For example, there are two drinks in a store: Coffee and tea. Their brewing method

Step 1: Always use boiling water

Step 2: Soak in coffee or tea with hot water

Step 3: pour the drink into the cup

Step 4: Add appropriate spices to your drinks

In the preceding steps, steps 1 and 3 are the same. They can be extracted and placed in the base class. The two and four are different. They can be abstracted and implemented by sub-classes. below is the abstract parent class of beverage, the parent class allows an unknown subclass to do things that may not be done well or cannot be completed at all. You can add a hook function: custcomerWantsCondiments (), and rewrite the Child class.

CoffeineBeverageWithHook. java

Package com. dzt. template;/*** because some customers may not need spices, they need to choose whether to add them, required ** @ author Administrator **/public abstract class CoffeineBeverageWithHook {void prepareRecipe () {boilWater (); brew (); pourInCup (); if (custcomerWantsCondiments ()) addCondiments ();} abstract void brew (); // brewing abstract void addCondiments (); // Add seasoning // boil water void boilWater () {System. out. println ("CoffeineBeverageWithHook ------------------> boilWater");} // pour the drink into the cup void pourInCup () {System. out. println ("CoffeineBeverageWithHook ------------------> pourInCup");}/*** subclass can overload this function ** @ return */boolean custcomerWantsCondiments () {return true ;}}

The following is the coffee class that inherits the parent class of the above beverage.

CoffeineWithHook. java

package com.dzt.template;public class CoffeineWithHook extends CoffeineBeverageWithHook {@Overridevoid brew() {// TODO Auto-generated method stubSystem.out.println("CoffeineWithHook-------------------->brew");}@Overridevoid addCondiments() {// TODO Auto-generated method stubSystem.out.println("CoffeineWithHook-------------------->addCondiments");}@Overrideboolean custcomerWantsCondiments() {// TODO Auto-generated method stubreturn false;}}

The following is a tea class that inherits the parent class of the above Beverage

TeaWithHook. java

package com.dzt.template;public class TeaWithHook extends CoffeineBeverageWithHook {@Overridevoid brew() {// TODO Auto-generated method stubSystem.out.println("TeaWithHook-------------------->brew");}@Overridevoid addCondiments() {// TODO Auto-generated method stubSystem.out.println("TeaWithHook-------------------->addCondiments");}@Overrideboolean custcomerWantsCondiments() {// TODO Auto-generated method stubreturn true;}}


The above code can be used to modify the custcomerWantsCondiments () function. The user determines whether to add spices.

Test code

Package com. dzt. template;/*** Add the Hook method to the template method, let the user decide ** @ author Administrator * @ date 2014.08.20 */public class TemplateMain {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubCoffeineWithHook coffHook = new CoffeineWithHook (); TeaWithHook teaHook = new TeaWithHook (); coffHook. prepareRecipe (); System. out. println ("---------------------------------------------------------------"); teaHook. prepareRecipe ();}}

Summary:

1. The template method is a basic technology for code reuse. They are particularly important in the class library. they extract the public behavior in the class library.
2. The template method leads to a direction control structure. "Hollywood law": "Don't call me, I will call you. ", that is, a parent class calls the subclass operation, rather than the opposite.
3. The template call operation types include specific operations, specific AbstracClass operations, primitive operations, factory methods, and hook operations. Less primitive operations.
4. The template method uses inheritance to change part of the algorithm. The rule mode uses delegation to change the entire algorithm.

Code: http://download.csdn.net/detail/deng0zhaotai/7835511

Reference books: Head First design patterns


What are the advantages of the software design pattern template pattern?

The Template Method Pattern Defines the algorithm skeleton in an operation, and delays some implementation steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm structure.
The template method mode is a simple design mode, but it is a basic technology for code reuse. It is particularly important in class libraries. It follows the principle that "abstract classes should have as many behaviors as possible, we should have as few data as possible. Methods used as templates should be defined in the parent class and abstract methods should be used in the definition of methods. However, the abstract methods of the parent class do not know how to deal with them, in practice, the specific processing is a subclass that implements specific functions in the subclass. Therefore, different subclass executions will produce different implementation results, but the processing process is customized according to the parent class. This is the essence of the template method. The algorithm skeleton is developed to implement sub-classes.

We recommend that you read an excellent book on design patterns: "software secrets-the point of design patterns" Edited by Zheng AQI. The descriptions are in place, and the instances are easy to understand!

I wish you an early learning of the design model!

Which of the following heroes can give examples of how to apply the template method in the Design Pattern in practice? Thank you!

Currently, the popular EJB model is a typical application example. It implements the model from the perspective of application technology. 3. The extension of the MVC design pattern is implemented in ASP. NET, which has excellent performance.

Related Article

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.