A Preliminary Study on the template mode of Android development and a preliminary study on the android Template

Source: Internet
Author: User

A Preliminary Study on the template mode of Android development and a preliminary study on the android Template

I think the template mode is the longest used in Android development and can be seen everywhere. Understanding this mode helps us to have a deeper understanding of the Android source code and framework. What is the template mode? The template mode defines a basic framework and delays some of the methods in implementation in the subclass. For example, when we go to a restaurant to have dinner, the basic steps are to find a seat, order, serve, eat, and pay for the five processes. The ordering process is variable, and we can order more or less dishes, the others are fixed, so we can write them in a fixed abstract class to form a basic framework. Our subclass inherits the abstract class and rewrites the variable methods.

Here is an example.

/** Abstract base class, provides an algorithm framework for all sub-classes ** refreshing drinks */public abstract class RefreshBeverage {/** template method for preparing drinks * encapsulates the algorithm framework followed by all sub-classes */public final void prepareBeverageTemplate () {// Step 1 boil boilWater (); // Step 2 brew (); // step 3 pour the drink into the cup pourInCup (); if (isCustomerWantsCondiments ()) {// step 4 Add the seasoning addCondiments () ;}/ ** Hook, Hook function, provides a default or empty implementation * specific sub-classes can decide whether to hook and how to hook * Ask the user whether to add spices */protected boolean isCustomerWantsCondiments () {return true ;} /** basic method: Boil water */private void boilWater () {System. out. println ("boil water");}/** basic method, pour the drink into the cup */private void pourInCup () {System. out. println ("pour a drink into a cup");}/** basic abstract method, bubble beverage */protected abstract void brew ();/** basic abstract method, add seasoning */protected abstract void addCondiments ();}

We have defined a template class for beverage preparation. Among them, we must note that the keyword of the prepareBeverageTemplate () method must be final to ensure that the subclass can only modify the visible method and ensure the stability of the template framework, the method that requires subclass implementation is defined as the Protected attribute and is an abstract class, which can be used freely by the subclass. Just like every class of Android will inherit the Activity class and rewrite the oncreate () the method is the same, and its attribute is also protected. methods that do not want to be visible in sub-classes, we need to position them as private attributes and write the method body.

We can also find that we define an isCustomerWantsCondiments () method in it. This method is the hook, which increases the flexibility of the template framework. This method can be rewritten in the subclass, to determine whether a method needs to be executed.

Public class Tea extends RefreshBeverage {@ Overrideprotected void brew () {System. out. println ("soak tea for 5 minutes with 80 degrees of Hot Water");} @ Overrideprotected void addCondiments () {System. out. println ("add Lemon");} @ Override/** select the mounting hook function by overwriting the subclass * @ see com. imooc. pattern. template. refreshBeverage # isCustomerWantsCondiments () */protected boolean isCustomerWantsCondiments () {return false ;}}
We wrote a tea class and returned false after the hook method was rewritten. The fourth step of preparing the beverage was canceled.

When do we need to use a template class?

(1) When algorithms or operations follow similar logic

(2) During refactoring (pumping the same code into the parent class), improving program reusability

(3) important and complex algorithms. The core algorithms are designed as template algorithms.


Which of the following is a good example of Google Android SDK development?

I personally think that if you are a newbie and want to learn about Android and do not use the mars video tutorial or platform development, you will have a great trip, if you need to make something to illustrate how to cover more things, you can give a lot of reference. Weak wood has little access to or comes into contact with Android, just like a couple of such drops or two get started, eight times. Example: Is there an electronic file platform development? Even wood has a bottom ....

Android sdk development examples

I have a complete example of Google Android SDK development. I don't know if you want it, but I forgot where it is. You can leave an email to send it to you.

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.