Template mode for Java design patterns

Source: Internet
Author: User

In the book "Java and Patterns" of Dr. Shanhong, this describes the template method pattern:

  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.

Structure of the template method pattern

  The template method pattern is one of the most common patterns in all patterns and is the basic technique for code reuse based on inheritance.

The template method pattern requires collaboration between the designer of the abstract class and the specific subclass. A designer is responsible for giving the outline and skeleton of an algorithm, while others are responsible for the various logical steps of the algorithm. The methods that represent these specific logical steps are called basic methods (primitive method), and the method of summarizing these basic methods is called template method, and the name of the design pattern is from here.

The behavior represented by a template method is called a top-level behavior, and its logic is called top-level logic. The static structure diagram for the template method pattern is as follows:

There are two characters involved:

  Abstract template roles have the following responsibilities:

  One or more abstract operations are defined to allow subclasses to implement. These abstractions are called basic operations, and they are the constituent steps of a top-level logic.

Defines and implements a template method. This template method is generally a concrete method, it gives a top-level logic of the skeleton, and the logical composition of the steps in the corresponding abstract operation, deferred to the sub-class implementation. It is also possible for top-level logic to invoke some concrete methods.

  The concrete template role also has the following responsibilities:

  Implement one or more of the abstract methods defined by the parent class, which are the constituent steps of a top-level logic.

Each abstract template role can have any number of specific template roles corresponding to it, and each specific template role can give a different implementation of these abstract methods (that is, the constituent steps of top-level logic), so that the implementation of the top-level logic varies.

Source
 package   cn.happy.a;  public  abstract  class   Animal { //  The basic method declaration (implemented by a subclass)  public  abstract     Span style= "COLOR: #000000" > String info ();  //  template method   Public  void   show () { // info () Call basic method  System.out.println (info () +" I'm the smartest animal "    Span style= "color: #000000"); }}
 package   cn.happy.a;  public  class  Cat extends   animal{ //  basic method implementation   @Override  public   String info () { return " I am a cat "; }}
 package   cn.happy.a;  public  class  Dog extends   animal{ //  basic method implementation   @Override  public   String info () { return " I am a dog "; }}
 Package cn.happy.a;  Public class Test {    publicstaticvoid  main (string[] args) {        Animal a1=  New  Dog ();        A1.show ();        Animal a2=new  Cat ();        A2.show ();    }}

The concrete template role class implements the basic method declared by the parent class, and the Abstractmethod () method represents the remaining logic that enforces the subclass implementation, and the Hookmethod () method is the logic that can be chosen to implement, not the one that must be implemented.

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.

Methods in the template method pattern

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

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.

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.

Template mode for Java design patterns

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.