Android design mode-Template method mode

Source: Internet
Author: User

To the Gome interview with Android, ask me about the design pattern related issues:

1. What is the meaning of the singleton model?

2, what kinds of factory method mode;

3, you used the template method mode, for example;

I feel a mess to answer. Template method mode is not spoken;

Tragedy!

The foundation is not strong, shake.

Big companies focus on the basics, so for Java basics, design patterns, algorithms. These are sure to be firm!


1. Definition:

Defines the skeleton of an operation algorithm that extends some of the steps into subclasses.
The template method pattern allows subclasses to redefine some of the steps of the algorithm without altering the algorithm structure.

Defines the skeleton of a algorithm in a method, deferring some steps to subclasses.

Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm ' s structure.

2. Function:

By using the template method pattern, you can encapsulate the implementation steps of some complex process in a series of basic methods, provide a method called template method in the abstract parent class to define the order of execution of these basic methods, and override some steps by its subclasses, so that the same algorithm framework can have different execution results.

3, template class, template method:

3.1, the template class can be abstract class, can also be a concrete class, according to the need to determine.

3.2, template class template method, must be concrete method, other methods can make the abstract method can also be a concrete method;

3.3, through inheritance and polymorphism control, can achieve sub-class to the parent-child reverse control;


4. Demo

Package Com.example.demo.templatemethod;import android.util.log;/** * Template Method abstract class * @author Qubian * @data June 9, 2015 * @email [Email protected] * */public abstract class Templatelotteryabs {/** * get random number */protected abstract String getrandomnum (int n UM);/** * Processes the data generated */protected abstract void dolotterystring (String lottery),/** * Calculates the number of bets */protected abstract void Dolott Erynum ();/** * is marked red * @return */public boolean shouldsetstringred () {return false;} /** * marked red */protected void setstringred () {log.i ("Templateabs", "setstringred");} /** * Template method: defined in an abstract class and completely inherited by subclasses without modification. The template method is a concrete method, which gives a top-level logical framework, and the logical component steps can be either concrete or abstract methods in an abstract class. * Because the template method is a concrete method, the abstraction layer in the template method pattern can only be an abstract class, not an interface. *  */public void doinfo (int num) {//Internal control dolotterystring (Getrandomnum (num));d olotterynum ();//This can be overridden by inheritance, Reverse control of the parent class's method if (shouldsetstringred ()) {setstringred ();}}}

Specific methods:

Package Com.example.demo.templatemethod;import android.util.log;/** * Template Method Subclass * @author Qubian * @data June 9, 2015 * @email [ Email protected] * */public class Templatelottery extends templatelotteryabs{private static final String TAG = "Templateobj "; @Overrideprotected String getrandomnum (int num) {return" 1,2,3,4,5,6 ";} @Overrideprotected void dolotterystring (String lottery) {log.i (TAG, "dolotterystring");} @Overrideprotected void Dolotterynum () {}/** * Reverse control */@Overridepublic Boolean shouldsetstringred () {return Super.shouldsetstringred ();} @Overrideprotected void setstringred () {super. Setstringred ();}}

Use:

Package com.example.demo.templatemethod;/** * Using template Method mode * @author Qubian * @data June 9, 2015 * @email [EMAIL protected] * */ public class Usetemplate {public void use () {templatelotteryabs tempobj= new templatelottery (); Tempobj.doinfo (5);}}

7, in the use of Android:

In the Android source code, the draw () method in view is a "template method".
When inheriting the view subclass, if you want to override or extend this method, the entire method flow and the basic content can not be modified,

View view is built by the view itself, where the algorithm implementation process is determined;


Subclasses can only extend the OnDraw (canvas canvas) and dispatchdraw (canvas canvas) Two functions so that the subclass's own view display is different from other specific subclasses.
Among them are: The TextView class overrides the OnDraw function, the ViewGroup class, Surfaceview overrides the Dispatchdraw () function, and so on.


public class View implements Drawable.callback, Keyevent.callback,        accessibilityeventsource {    /**     * Implement this to do your drawing.     *     * @param canvas the canvas on which the background'll be drawn     *    /protected void OnDraw (canvas canvas) {
   }    /** * Called by draw to draw the ' child ' views     . This is overridden     * by derived classes-gain control just before its children is drawn     * Own view has been drawn).     * @param canvas the canvas on which to draw the view     *    /protected void Dispatchdraw (canvas canvas) {    }}


public class TextView extends View implements Viewtreeobserver.onpredrawlistener {    @Override    protected void OnDraw (canvas canvas) {        restartmarqueeifneeded ();        Draw the background for this view        Super.ondraw (canvas);        Final Drawables dr = Mdrawables;        if (dr! = null) {             //...        }        int color = Mcurtextcolor;        Mtextpaint.setcolor (color);        Mtextpaint.drawablestate = Getdrawablestate ();        Canvas.save ();    }



Android design mode-Template method mode

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.