Template Method mode used in android

Source: Internet
Author: User

Template Method mode used in android

Template Method ):

The template method mode is the behavior mode of the class. An abstract class is provided to implement part of the logic in a specific method or constructor form, and then some abstract methods are declared to force the subclass to implement the remaining logic code. Different sub-classes can implement these abstract methods in different ways, so the residual logic of sub-classes is different. In addition, the template method mode is based on the Inheritance Mechanism code reuse technology. Its structure and usage are also the core of object-oriented design.

The simple class chart structure of the template method mode is as follows:

The template method involves several roles:

1. Abstract template role

A. Declare one or more abstract methods for subclass implementation. These methods are "basic methods" and they are the building steps of a top-level logic.

B. Declare and implement a template method. This method is generally a specific method and provides a framework of top-level logic. The specific logical composition steps are postponed to subclass implementation.

2. Specific template roles

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

B. Each abstract template role corresponds to a specific template role, and each specific template role can provide different implementations of these corresponding abstract methods.

 

Now, we will describe the usage of the template method using an instance. The example is as follows: In android, we often use the Inheritance Mechanism to define a base class BaseActivity for all Activity classes. In this way, the general logic of activity can be encapsulated in the base class, to achieve code reuse, we can use the template method mode. The specific structure class diagram is shown below.

 

The following is the core code of the template method mode:

Abstract template role:

/**

*@ Description:

* Abstract template role-parent class of all child classes

*/

Public abstract classBaseActivityExtendsActivity {

 

@ Override

Protected voidOnCreate (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);

 

// Execute the initialization page

InitView ();

}

 

/**

*@ Description:

* Lifecycle method-initialization page execution

*/

Protected abstract voidInitView ();

 

/**

*@ Description:

* Life Cycle method-re-enter the page for execution

*/

Protected abstract voidResume ();

 

/**

*@ Description:

* Life Cycle method-hide to the desktop or be suspended for execution

*/

Protected abstract voidPause ();

 

/**

*@ Description:

* Life Cycle method-exit the current page and execute

*/

Protected abstract voidDestroy ();

 

@ Override

Protected voidOnDestroy (){

Super. OnDestroy ();

Destroy ();

}

 

@ Override

Protected voidOnPause (){

Super. OnPause ();

Pause ();

}

 

@ Override

Protected voidOnResume (){

Super. OnResume ();

Resume ();

}

 

}

 

Template Method role:

/**

*@ Description:

* Specific template roles-specific implementation Abstract METHODS

*/

Public classLoadingActivityExtendsBaseActivity {

 

@ Override

Protected voidInitView (){

SetContentView (R. layout.Activity_loading);

//TODO

Log (initialization page execution );

}

 

@ Override

Protected voidResume (){

Log (recovery page execution );

}

 

@ Override

Protected voidPause (){

Log (pause page execution );

}

 

@ Override

Protected voidDestroy (){

Log (page destruction execution );

}

 

Private voidLog (String msg ){

Log.D(Patterns, msg );

}

}

 

The following figure shows the running result of the template mode instance:

 

 

 

 

 

 

 

 

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.