Research on the template mode of Android development

Source: Internet
Author: User

Template mode I think in the development of Android is the longest use, basically is ubiquitous, know this mode, help us to Android's source code and framework have a deeper understanding. What is template mode, template mode is the definition of a basic framework in which some of the methods are deferred to the implementation of subclasses. For example, we go to a restaurant to eat, the basic steps must be to find a seat, a la carte, serve, eat, pay five process, which the process is variable, we order the dishes can be less, the others are fixed, then we can write in a fixed abstract class to form a basic framework, our subclasses inherit the abstract class, The variable method is replicated.

Let's look at an example below.

/* * Abstract base class, providing an algorithmic framework for all subclasses * Refreshing drink */public abstract class Refreshbeverage {/* * Preparing beverage Template Method * Encapsulates the algorithm framework that all subclasses follow together */public final void preparebeveragetemplate () {//step 1 to boil water boilwater (); /Step 2 Brewing beverage brew ();//step 3 Pour the drink into the cup pourincup (); if (Iscustomerwantscondiments ()) {//Step 4 Add seasoning addcondiments ();}} /* hook, hook function, provide a default or empty implementation * Specific subclasses can decide whether to hook and how to hook * Ask the user to add seasoning */protected Boolean iscustomerwantscondiments () {return true;} /* Basic method, boil the water */private void Boilwater () {System.out.println ("boil Water");} /* Basic method, pour the drink into the cup */private void Pourincup () {System.out.println ("Pour the drink into the Cup");} /* Abstract basic method, bubble drink */protected abstract void Brew ();/* Abstract basic method, add seasoning */protected abstract void addcondiments ();}  

We define a template class for the preparation of beverages, in which we must note that the keyword of the Preparebeveragetemplate () method must be final, guaranteeing that the subclass can only modify the visible method to ensure the stability of the template frame. A method that requires a subclass implementation is defined as a protected attribute and is an abstract class that is free to play by subclasses, just as each of our Android classes inherits the activity class, and the OnCreate () method is replicated. Its properties are also protected. A method that does not want to be visible in a subclass, we need to position it as a private property and write a good method body.

We can also find out where we define a iscustomerwantscondiments () method, which is the hook hook, which increases the flexibility of the template framework, which can be overridden in subclasses to determine whether a method needs to be executed.

public class Tea extends Refreshbeverage {@Overrideprotected void Brew () {System.out.println ("Soak tea with 80 degrees of hot water for 5 minutes");} @Overrideprotected void Addcondiments () {System.out.println ("Add lemon"); @Override/* Subclass Select the Mount hook function in the form of overrides * @see com.imooc.pattern.template.refreshbeverage#iscustomerwantscondiments () */ Protected Boolean iscustomerwantscondiments () {return false;}}
we wrote a tea class and returned the hook method to false, eliminating the fourth step of preparing the drink.

So when do we need to use a template class?

(1) When the algorithm or operation follows similar logic

(2) When refactoring (pull the same code into the parent class), improve the reusability of the program

(3) Important complex algorithm, core algorithm design for template algorithm

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.