Template Method--Class behavior pattern

Source: Internet
Author: User

The intent of the template method mode:

Defining the skeleton of an algorithm in an operation, Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. That is, the parent class has defined the process of the algorithm, and the subclass only needs to change the implementation of some of these steps.

Just like we're going to put things in the freezer, a complete set of processes is needed, open the fridge, put the items, and close the refrigerator. The opening and closing are fixed, and the items placed are indeterminate, so the items that we put in are required to be implemented in subclasses. After instantiating the object, you can complete the process by calling the process method (Templetemethod) in the parent class. This is the result of a reverse control of the template method, that is, the subclass to find the method of the parent class. The specific code will be implemented later.

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

AbstractClass abstract class implements a template method, defines an algorithm skeleton, the template method can call the method in the AbstractClass class, can also call methods in other classes.

Concreteclass the steps associated with implementing some specific subclasses in the parent class operation.

The Java code of the template method pattern is implemented as follows:

Define a AbstractClass template method and other template fixation methods, as well as the abstract methods that subclasses need to modify.

<pre name= "code" class= "java" >package templatemethod;/** * to put things in the refrigerator example */public abstract class AbstractClass {// Template method public void Templetemethod () {open ();p utobject (); Close ();} Open the refrigerator public void open () {System.out.println ("Open refrigerator ..."); Put things public abstract void putObject ();//off refrigerator public void close () {System.out.println ("off the refrigerator ...");}


Abstract methods implemented by two specific subclasses:

Package Templatemethod;public class Concreteclassa extends abstractclass{@Overridepublic void PutObject () { System.out.println ("Put fruit in the fridge ...");}
Package Templatemethod;public class Concreteclassb extends AbstractClass {@Overridepublic void PutObject () { System.out.println ("Put vegetables in the fridge ...");}
Test class:

Package Templatemethod;public class Testmain {public static void main (string[] args) {AbstractClass a;// Instantiate Concreteclassa () a = new Concreteclassa (); A.templetemethod ();//Instantiate CONCRETECLASSBA = new Concreteclassb (); A.templetemethod ();}}
applicability of template method mode:

1, one-time implementation of an invariant part of the algorithm, and the variable portion is left to the subclass to achieve.

2. The public behavior of each subclass should be extracted and centralized into a common parent class to avoid duplicate code.

3, control sub-class extension.

Summarize:

The template method pattern is a class behavior pattern that is independent of the object. and the template method pattern can be used to find the parent class by subclasses. He can reduce a lot of repetitive code, so in a lot of frameworks, the use of template method is more, the most classic is in the use of spring, I hope you can learn to understand.

Template Method--Class behavior pattern

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.