The template method is used in Android applications.

Source: Internet
Author: User

The Android system is an excellent application framework. It is widely used in the framework design mode. Of course, Android is no exception. This article simply uses the View class as an example to describe the application of the template method mode in Android.

First, let's review the template method mode. The template method mode defines algorithms and processes in the base class. The specific implementation delay is defined in the derived class. The UML diagram is as follows:

We can see that the templatemethod method is defined in abstractclass, and the call to the primitiveoperation method is defined in this method, but the primitiveoperation method is defined in abstractclass as an abstract method without implementation, therefore, this method can only be a template and cannot be called. In this way, the primitiveoperation method needs to be defined when concreteclass is defined, so that the logic defined in the templatemethod method in abstractclass can be correctly run.

Through the above description, we can know that the template method will certainly be widely used in the framework. This mode can be defined in the framework according to the logic flow. The specific implementation is defined by the user.

Next I will take the view class in the android framework as an example to introduce the application of this mode in Android.

1. Create a helloview project.

2. Add a myview class with the following code:

package com.simon.helloview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.view.View;public class MyView extends View {public MyView(Context context) {super(context);}protected void onDraw (Canvas canvas) {Paint paint = new Paint();paint.setARGB(255, 255, 0, 0);canvas.drawCircle(100, 100, 100, paint);}}

3. The code for defining helloviewactivity is as follows:

package com.simon.helloview;import android.app.Activity;import android.os.Bundle;public class HelloViewActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new MyView(this));    }}

In this example, myview implements the ondraw method of the base class view. This method is protected and cannot be called by users. So who will call this method? If you carefully study the view. Java code, you will find that the ondraw method is called in the view draw method. The logic is as follows:

public void draw(Canvas canvas) {    .......    onDraw(canvas);    .......}

It can be seen that the template method has been defined in the base class view, so in the derived class, you only need to define the ondraw method to draw on the window. You can call the draw method of the view object template to draw a window.

This method is widely used in Android templates and can be seen everywhere. For example, the oncreate and onstart methods of the activity class are all defined in the base class, and these methods can be understood as the specific implementation of the derived class. When appropriate, the base class calls these methods in the base class according to its own logic.

In the process of software development, we need to study and analyze problems from the perspective of the design model, which greatly improves our ability to analyze problems.

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.