Template method mode used in Android

Source: Internet
Author: User

Template method Mode:

The template method pattern is the behavior pattern of the class. Provides an abstract class that implements some of the logic in a specific method or construct, and then declares some abstract methods that force subclasses to implement the remaining logic code. Different subclasses can implement these abstract methods in different ways, so the remaining logic of the subclass implementation is different. In addition, the template method pattern is based on inheritance mechanism code reuse technology, its structure and usage is also the core of object-oriented design.

The abbreviated class diagram structure of the template method pattern is as follows:

The template method pattern involves several roles:

1. Abstract template Role

A, declare one or more abstract methods, for subclasses to implement specifically, these methods are "basic methods", they are a top-level logic of the constituent steps.

B, declare and implement a template method, this method is generally a concrete method, gives the framework of the top-level logic, the specific logical composition of the steps deferred to the subclass implementation.

2. Specific template role

A. Implement one or more of the abstract methods provided by the parent class.

b, each abstract template role has a specific template role correspondence, and each specific template role can give these corresponding abstract methods of different implementations.

Well, the following also illustrates the use of template method patterns with an example. The example is this: in Android, we often use the inheritance mechanism to define a base class baseactivity for all activity classes, so that the activity-generic logic can be encapsulated in the base class, enabling code reuse. So we can use the template method pattern to achieve, the specific structure class diagram is as follows.

The following is the core Code section of the template method pattern:

Abstract template role:

/**

 * @description :

 * Abstract Template Role - Parent class for all subclasses

*/

Public Abstract class Baseactivity extends Activity {

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

// Basic Configuration

Requestwindowfeature (Window. Feature_no_title);

GetWindow (). Setsoftinputmode (Windowmanager.layoutparams. Soft_input_state_always_hidden);

// ...

Setcontentview (r.layout. Activity_base);

// Initialize page execution

Initview ();

}

/**

    * @description :

    * life cycle Methods-Initialize page execution

*/

protected abstract void initview ();

/**

    * @description :

    * life Cycle Method-re-entry page execution

*/

protected abstract void resume ();

/**

    * @description :

    * life cycle Methods-hidden to the desktop or suspended for execution

*/

protected abstract void pause ();

/**

    * @description :

    * life Cycle Method-leave the current page to execute

*/

protected abstract void destroy ();

@Override

protected void OnDestroy () {

Super. OnDestroy ();

Destroy ();

}

@Override

protected void onPause () {

Super. OnPause ();

Pause ();

}

@Override

protected void Onresume () {

Super. Onresume ();

Resume ();

}

}

Specific Template method roles:

/**

 * @description :

 * specific template roles-concrete implementation of abstract methods

*/

Public class Loadingactivity extends baseactivity {

@Override

protected void Initview () {

Setcontentview (r.layout. Activity_loading);

// TODO

Log (" Initialize page execution ");

}

@Override

protected void resume () {

Log (" Restore page execution ");

}

@Override

protected void pause () {

Log (" pause page Execution ");

}

@Override

protected void destroy () {

Log (" page Destruction Execution ");

}

private void log (String msg) {

Log. D ("Patterns",msg);

}

}

The following is the result of running the template method pattern instance:

Well, to here the visitor mode has been introduced, I hope to help you, in addition, the original work hard-won, reproduced please indicate thank you.

Click I download the code!

Technology Exchange Group:179914858

Template method mode used in Android

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.