[Original · tutorial · serialization] "Android's big talk Design Pattern"-design pattern-behavior pattern Chapter 2: template method pattern the person's life should spend like this

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

 

Template Method Mode A person's life should be spent like this 

Template Method ModeUse Cases: 

One day, GG and mm seriously said that a person's life is basically divided into the following stages:

Childhood: The primary school task for children in childhood is to learn to speak.

Children stage: people need to perform basic learning.

Juvenile stage: people are still learning, and learning is more difficult.

Youth stage: learning and work.

Middle-aged stage: Summarize previous studies and learn more from them.

Elder care: the elderly need to be calm and learn. Learning will delay aging.

Gg said that every stage of life has its own characteristics and things to do. We are both in the early stages of youth, so we must study hard. Of course, we must also cherish my true feelings, because tomorrow is unpredictable, we will love each other today.

Template Method mode explanation: 

Template Method pattern: defines an operationAlgorithmAnd delay the execution of some steps (or basic methods) to its subclass, this allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

Define the skeleton of an algorithm in an operation, deferring some
Steps to subclasses. template method lets subclasses redefine certain steps
An Algorithm without changing the algorithm's structure ..

Template MethodUMLFigure: 

The role involved in the template method mode is as follows:

Abstract class: defines one or more abstract methods for specific subclasses to implement them. It also implements a template method to define the skeleton of an algorithm. This template method not only calls the preceding abstract method, but also calls other operations as long as it can fulfill its mission.

Concreteclass: implements the abstract methods in the parent class to complete the steps related to the specific subclass in the algorithm.

The UML diagram of the combination mode is as follows:

 

In-depth analysis of template method Modes:

Some people say that if you only use one design pattern, this design pattern must be the template method pattern.

The template method mode is a very simple design mode, but it is also widely used. The template method mode uses the inheritance method to realize the heterogeneity of algorithms. The key point is to encapsulate general algorithms in abstract base classes and put different algorithm details into sub-classes for implementation.
Template Method mode to obtain a reverse control structure, which is also a principle in the analysis and design of object-oriented systems Dip (dependency inversion: Dependency inversion
Principles ). Its meaning is that the parent class calls the sub-class operation (the high-level module calls the operation of the lower-level module), and the lower-level module implements the interface declared by the high-level module. In this way, the control is in the parent class (high-level module), and the lower-level module depends on the higher-level module instead.

Template Method mode use scenario analysis andCodeImplementation: 

In the above application scenario, a person can look at the abstract class throughout his life, while mm and GG are specific classes. The UML diagram is as follows:

Create an abstract template:

PackageCom. diermeng. designpattern. templatemethod;

/*

* Person's life

*/

Public Abstract ClassLife {

// Childhood

Public Abstract VoidBabyhood ();

 

// Childhood

Public Abstract VoidChildhood ();

 

// Juvenile

Public Abstract VoidYoungster ();

 

// Childhood

Public Abstract VoidYouth ();

 

// Childhood

Public Abstract VoidMiddle ();

 

// Juvenile

Public Abstract VoidElderly ();

 

Public VoidLive (){

This. Babyhood ();

This. Childhood ();

This. Youngster ();

This. Youth ();

This. Middle ();

This. Elderly ();

}

}

 

Create a GG class:

PackageCom. diermeng. designpattern. templatemethod. impl;

 

ImportCom. diermeng. designpattern. templatemethod. Life;

 

/*

* Gg template implementation

*/

Public ClassGgExtendsLife {

 

Public VoidChildhood (){

 

System.Out. Println ("GG: People in the Children stage need to perform basic learning. ");

}

 

Public VoidBabyhood (){

System.Out. Println ("GG: The primary school task for children is to learn to speak. ");

}

 

Public VoidYoungster (){

System.Out. Println ("GG: teenagers are still learning, and learning is more difficult. ");

}

 

 

Public VoidElderly (){

 

System.Out. Println ("GG: elderly people need to be calm and learn. Learning will delay aging. ");

}

 

Public VoidMiddle (){

System.Out. Println ("GG: In the middle-aged stage, we mainly summarize previous studies and learn more from them. ");

 

}

 

Public VoidYouth (){

 

System.Out. Println ("GG: the main task of learning and working in the youth stage. ");

 

}

 

}

 

Create a specific mm class:

PackageCom. diermeng. designpattern. templatemethod. impl;

 

ImportCom. diermeng. designpattern. templatemethod. Life;

 

/*

* Mm template implementation

*/

Public ClassMmExtendsLife {

 

Public VoidChildhood (){

 

System.Out. Println ("mm: basic learning is required for children. ");

}

 

Public VoidBabyhood (){

System.Out. Println ("mm: The primary school task for children is to learn to speak. ");

}

 

Public VoidYoungster (){

System.Out. Println ("mm: teenagers are still learning, and learning is more difficult. ");

}

 

 

Public VoidElderly (){

 

System.Out. Println ("mm: elderly people need to be calm and learn. Learning will delay aging of the elderly. ");

}

 

Public VoidMiddle (){

System.Out. Println ("mm: In the middle-aged stage, we mainly summarize previous studies and learn more from them. ");

 

}

 

Public VoidYouth (){

 

System.Out. Println ("mm: the main task of learning and working in the youth stage. ");

 

}

 

}

 

Create a test client:

PackageCom. diermeng. designpattern. templatemethod. client;

ImportCom. diermeng. designpattern. templatemethod. Life;

ImportCom. diermeng. designpattern. templatemethod. impl. GG;

ImportCom. diermeng. designpattern. templatemethod. impl. mm;

 

/*

* Test the client

*/

Public ClassTemplatemethodtest {

Public Static VoidMain (string [] ARGs ){

System.Out. Println ("-----------------------------------------------------");

Life Gg =NewGG ();

 

GG. Live ();

 

System.Out. Println ("-----------------------------------------------------");

 

Life Mm =NewMm ();

Mm. Live ();

 

System.Out. Println ("-----------------------------------------------------");

 

}

}

The running result is as follows:

-----------------------------------------------------

GG: The primary school task for young people is to learn to speak.

GG: People in the Children stage should perform basic learning.

GG: teenagers are still learning, and learning is more difficult.

GG: the main task of learning and working in the youth stage.

GG: In the middle-aged stage, we mainly summarize previous studies and learn more from them.

GG: elderly people need to be calm and learn. Learning will delay aging.

-----------------------------------------------------

MM: The primary school task for young people is to learn to speak.

MM: basic learning is required for children.

MM: teenagers are still learning, and learning is more difficult.

MM: The main task of learning and working in the youth stage.

MM: In the middle-aged stage, we mainly summarize the previous learning and learn more from it.

MM: elderly people need to be calm and learn. Learning will delay aging.

-----------------------------------------------------

Advantages and disadvantages of the template method mode: 

Advantages:

Using the template method mode, while defining the algorithm skeleton, You can flexibly implement specific algorithms to meet your flexible needs.

Disadvantages:

Although the template method mode can be used to implement specific algorithms freely, if the algorithm skeleton is changed, this requires changing the abstract class.

Introduction to the actual application of the template method mode: 

The template mode applies to the following situations:

First, implement the unchanged part of an algorithm at a time and leave the variable behavior to the subclass for implementation.

Second, the common behaviors in each subclass should be extracted and centralized into a common parent class to avoid code duplication. In fact, this is a good coding habit.

Third, control subclass extension. The template method only calls the operation at specific points, so that the extension can only be performed at these points. If you do not want subclass to modify the framework of your template method definition, you can do this in two ways: first, your template method is not reflected in the API; 2. Set your template method to final. We can see that the template method can be used to extract the public behavior of the Code for reuse. In the template method mode, the template method of the parent class controls the specific implementation of the subclass. In this way, you do not need to have much knowledge about business processes when implementing child classes.

Tip: 

The template method mode is a widely used design mode. It is widely used in Servlet and many well-known frameworks. In general, when we use the framework, it is often the framework that has designed the algorithm skeleton for us and also implemented some transactions and permission control, what we need to do is follow the pre-planned process to implement the corresponding business methods.

for a person's life, each stage has its own focus. For Gg and mm, it must be well learned. At the same time, they should try to love each other. Shaohua is easy to die. O (shao_yu) O Haha ~

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.