Template mode I think in the development of Android is the longest use, basically is ubiquitous, know this mode, help us to the Android source code and framework have a deeper understanding. What is template mode, template mode is the definition of a basic framework that delays some of these methods to run implementations in subclasses. For example, we go to a restaurant to eat, the basic steps must be to find a seat, order, serve, eat, pay five process, the order of the process is variable, we order more than a few dishes, others are fixed, then we can write in a fixed abstract class to form a basic framework, our subclasses inherit the abstract class, Make a carbon copy of the variable method.
Let's take a look at the example below.
/* * Abstract base class, providing an algorithmic framework for all subclasses * Refreshing drink */public abstract class Refreshbeverage {/* * Preparing drinks Template Method * Encapsulates the algorithm framework that all subclasses follow together */public final void preparebeveragetemplate () {//step 1 to boil the 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 * Detailed subclasses are able to decide whether to hook and how to hook up * 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 for the preparation of beverages, in which we must pay attention to the Preparebeveragetemplate () method keyword must be final, to ensure that the subclass can only change the visible method, to ensure the stability of the template framework, A method that requires the implementation of a subclass 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 in the replication. Its properties are also protected. Methods that do not want to be visible in subclasses, we need to position them as private properties and write the method body.
We can also find that we have defined a iscustomerwantscondiments () method, which is the hook hook, which increases the flexibility of the template framework, which can be overridden in subclasses to infer whether a method needs to be run.
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
Research on the template mode of Android development