Java Design Pattern Series (13) Template method

Source: Internet
Author: User

Java Design Pattern Series (13) Template method

The template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern.

I. Structure of the template method

    • AbstractClass: abstract class. Used to define the algorithm skeleton and primitive operations, and the specific subclasses implement each step of an algorithm by redefining these primitive operations. In this class, you can also provide a generic implementation of the algorithm.

    • ConcreteClass: a concrete implementation class. Used to implement some of the steps in the skeleton of an algorithm to accomplish the functions associated with a particular subclass.

Source

(1) abstract Template role Class

/**   * Define Template method, Abstract classes such as primitive operations   */ public  abstract  class  abstracttemplate {/** primitive operation 1, the so-called primitive operation is an abstract operation, it must be provided by subclasses to implement */ public  abstract  void   ();    /** primitive operation 2 */ public  abstract  void  doprimitiveoperation2     ();    /** template method, define algorithm skeleton */ public  final  void  templatemethod         () {doprimitiveoperation1  (); doprimitiveoperation2     (); }}

(2) Specific template role classes

/** * 具体实现类,实现原语操作 */publicclassextends AbstractTemplate {    publicvoiddoPrimitiveOperation1() {        //具体的实现    }    publicvoiddoPrimitiveOperation2() {        //具体的实现    }}

The key to template mode is that subclasses can displace the mutable parts of the parent class, but subclasses cannot change the top-level logic represented by the template method.

Whenever a new subclass is defined, do not think in terms of the flow of control, but in accordance with the "responsibility" thinking. In other words, you should consider which operations must be replaced, which operations can be displaced, and which actions are not replaceable. Using template mode can make these responsibilities clear.

Second, summary

The methods in the template method can be divided into two main categories: Template method and Basic method.

(1) Template method

A template method is defined in an abstract class that combines basic operational methods to form a total algorithm or a method of total behavior.

An abstract class can have any number of template methods, not limited to one. Each template method can invoke any number of specific methods.

(2) Basic methods

The basic method can be divided into three kinds: abstract method, Concrete method (concrete methods) and hook method.

    • Abstract method: An abstract method is declared by an abstract class and implemented by a specific subclass. In the Java language, abstract methods are indicated by the Abstraction keyword.

    • Concrete method: A concrete method is declared and implemented by an abstract class, and the subclass is not implemented or replaced.

    • Hook method: A hook method is declared and implemented by an abstract class, and subclasses are extended. Usually the implementation given by an abstract class is an empty implementation, as the default implementation of the method.

Record a little bit every day. Content may not be important, but habits are important!

Java Design Pattern Series (13) Template method

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.