Android design mode-template method mode

Source: Internet
Author: User

Android design mode-template method mode

When I interviewed Android at Gome, I asked my questions about the design model:

1. What is the significance of Singleton mode;

2. Which factory method modes are available;

3. Examples of the template method you have used;

I feel a mess of answers. The template method mode is not mentioned;

Tragedy!

The foundation is not strong, and the ground is shaking.

Large companies pay attention to the basics, so they are concerned with java basics, design patterns, and algorithms. These must be solid!

 

1. Definition:

Defines the skeleton of an operation algorithm and extends some steps to sub-classes.
The template method mode allows the subclass to redefine certain steps of the Algorithm without changing the algorithm structure.

Defines the skeleton of an 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. functions:

By using the template method mode, you can encapsulate the implementation steps of some complex processes in a series of basic methods, in the abstract parent class, a method called the template method is provided to define the execution sequence of these basic methods, and its subclass is used to override certain steps, so that the same algorithm framework can have different execution results.

3. template class, template method:

3.1 The template class can be an abstract class or a specific class, which can be determined as needed.

3.2 The template method in the template class must be a specific method. Other methods can make the abstract method or a specific method;

3.3. Inheritance and polymorphism control allow child classes to reverse control their parent and child classes;

 

 

4. demo

 

Package com. example. demo. templateMethod; import android. util. log; /*** template method abstract class * @ author qubian * @ data June 9, 2015 * @ email naibbian@163.com **/public abstract class TemplateLotteryAbs {/*** obtain Random Number */protected abstract String getRandomNum (int num ); /*** process the data generated by the instant count */protected abstract void doLotteryString (String lottery);/*** calculate the number of notebooks */protected abstract void doLotteryNum (); /*** red? * @ return */publ Ic boolean shouldSetStringRed () {return false;}/*** marked red */protected void SetStringRed () {Log. I ("TemplateAbs", "SetStringRed");}/*** template method: It is defined in an abstract class and completely inherited by the subclass without modification. * The template method is a specific method that provides a top-level logical framework. * The logical composition step can be a specific method or abstract method in the abstract class. * Since the template method is a specific method, the abstract layer in the template method mode can only be an abstract class rather than an interface. **/Public void doInfo (int num) {// internal control doLotteryString (getRandomNum (num); doLotteryNum (); // This can be overwritten by inheritance, reverse Control of the parent class method if (shouldSetStringRed () {SetStringRed ();}}}

Specific Method:

 

 

Package com. example. demo. templateMethod; import android. util. log;/*** template method subclass * @ author qubian * @ data June 9, 2015 * @ email naibbian@163.com **/public class TemplateLottery extends TemplateLotteryAbs {private static final String TAG = "TemplateObj "; @ Overrideprotected String getRandomNum (int num) {return "1, 2, 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 ();}}

Usage:

 

 

Package com. example. demo. templateMethod;/*** use template method mode * @ author qubian * @ data June 9, 2015 * @ email naibbian@163.com **/public class UseTemplate {public void use () {TemplateLotteryAbs tempObj = new TemplateLottery (); tempObj. doInfo (5 );}}

7. Application in Android:

 

In the Android source code, the Draw () method in View is a "template method ".
If you want to override or extend this method in the inherited View subclass, the entire method flow and basic content cannot be modified,

The construction of the View is implemented by the View itself, and the algorithm implementation process is determined;


Child classes can only show different View effects of child classes and other specific child classes by extending the onDraw (Canvas canvas) and dispatchDraw (Canvas canvas) functions.
Among them, the TextView class overrides the OnDraw function, the ViewGroup class, And SurfaceView overrides the dispatchDraw () function.

 

 

public class View implements Drawable.Callback, KeyEvent.Callback,        AccessibilityEventSource {    /**     * Implement this to do your drawing.     *     * @param canvas the canvas on which the background will be drawn     */    protected void onDraw(Canvas canvas) {    }    /**     * Called by draw to draw the child views. This may be overridden     * by derived classes to gain control just before its children are drawn     * (but after its 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();    }


 

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.