Java design Pattern (17): Behavioral-Template Method mode

Source: Internet
Author: User

Scene
    • Customers to the bank to handle business:
    • Queue number
    • Specific cash/transfer/Corporate/personal/financial Services
    • Scoring for Bank staff

Core:
The code that handles a process already has it, but the code for one of the nodes is temporarily uncertain. Therefore, we use the factory method pattern, which transfers the code implementation of this node to the subclass completion.
That is, the process step is defined in the parent class, and the implementation is deferred to the subclass definition.

Overview
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.
For example, the skeleton of an algorithm in an operation is defined, and the steps are deferred to the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm.


roles in the pattern
Abstract class (AbstractClass): The template method is implemented, and the skeleton of the algorithm is defined.

Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm.


Public abstract class Bank {public void Takenumber () {System.out.println ("queue number");} /* Transact specific business */public abstract void Transact ();p ublic void Evaluate () {System.out.println ("Feedback Score");} Public final void process () {this.takenumber (); This.transact (); This.evaluate ();}} public class Client {public static void main (string[] args) {//TODO auto-generated method Stubbank Bank = new Bank () {@Ove rridepublic void Transact () {//TODO auto-generated method StubSystem.out.println ("I want to withdraw money");}; Bank.process ();}}

When to use template method mode:

When implementing an algorithm, the overall steps are fixed. However, some parts are variable, and the variable parts can be abstracted out for subclasses to implement.


Common scenarios in development:
Very frequently. Each frame, class library has its shadow. For example, the common ones are:
    • Encapsulation of database access
    • JUnit Unit Test
    • Servlet in about Doget/dopost method invocation
    • Hibernate in Template program
    • Spring in JdbcTemplate, hibernatetemplate, etc.

Java design Pattern (17): Behavioral-Template Method mode

Related Article

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.