JAVA design mode: Template mode; java design mode Template

Source: Internet
Author: User

JAVA design mode: Template mode; java design mode Template

In the book "JAVA and patterns" by Dr. Yan Hong, he first described the Template Method mode as follows:

  The template method mode is the behavior mode of the class. Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. This is the intention of the template method mode.

Structure of the template method mode

  The template method mode is one of the most common modes in all modes and is a basic technology based on inherited code reuse.

The template method mode requires collaboration between designers who develop abstract classes and specific sub-classes. One designer is responsible for providing the outline and skeleton of an algorithm, while other designers are responsible for providing the logical steps of this algorithm. The method that represents these specific logical steps is called the basic method. The method that summarizes these basic methods is called the template method ), the name of this design pattern is from here.

The behavior represented by the template method is called top-level behavior, and its logic is called top-level logic. The static structure of the template method mode is as follows:

Two roles are involved:

  Abstract Template has the following responsibilities:

  ■ One or more abstract operations are defined to implement sub-classes. These abstract operations are called basic operations, which constitute a step of top-level logic.

■ Defines and implements a template method. This template method is generally a specific method, which provides a skeleton of top-level logic, and the logical composition steps are postponed to subclass implementation in the corresponding abstract operations. The top-level logic may also call some specific methods.

  The role of a specific Template has the following responsibilities:

  ■ Implement one or more abstract methods defined by the parent class. They are the composition steps of a top-level logic.

■ Each abstract template role can have any number of specific template roles, and each specific template role can provide these abstract methods (that is, the composition steps of top-level Logic) so that the implementation of the top-level logic is different.

Source code
Package cn. happy. a; public abstract class Animal {// basic method declaration (implemented by subclass) public abstract String info (); // template method public void show () {// info () call the basic method System. out. println (info () + "I am the smartest animal ");}}
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 {    public static void main(String[] args) {        Animal a1=new Dog();        a1.show();        Animal a2=new Cat();        a2.show();    }}

 

The role class of the specific template implements the basic method declared by the parent class. The abstractMethod () method represents the residual logic implemented by the forced subclass, And the hookMethod () the method is optional implementation logic, not required.

The key to the template mode is:Subclass can replace the variable part of the parent class, but it cannot change the top-level Logic represented by the template method.

  When defining a new subclass, you should not think about the control process, but the "responsibility" approach. In other words, we should consider which operations must be replaced, which can be replaced, and which cannot be replaced. Using the template mode can clarify these responsibilities.

Methods In Template Method Mode

Template methods can be divided into two categories: Template methods and basic methods.

Template Method

A template method is defined in an abstract class and combines basic operation methods to form a general algorithm or a general-line method.

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

Basic Method

There are three basic methods: Abstract Method, Concrete Method, and Hook Method ).

Abstract method:An abstract method is declared by an abstract class and implemented by a specific subclass. Abstract keywords are used to mark abstract methods in Java.

Specific Method:A specific method is declared and implemented by an abstract class, but not implemented or replaced by a subclass.

Hook method:A hook method is declared and implemented by the abstract class, And the subclass is extended. An abstract class usually provides an empty implementation as the default Implementation of the 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.