Design Patterns Learning Notes (18: Template method Mode)

Source: Internet
Author: User

1.1 Overview

The skeleton of an algorithm in an operation is defined, and some steps are deferred to the subclass. The template method enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. This is the definition of the template method.

A method in a class is used to indicate the behavior of an instance of the class, a class can have many methods, and an instance method in a class can invoke several other methods in the class. When writing a class, it may be necessary to integrate many methods of a class into an instance method, that is, to encapsulate the invocation of several methods with an instance method to represent the skeleton of an algorithm, that is to say, invoking the instance method is equivalent to executing several methods in a certain order.

For example, all types of passenger stations carry out security checks, verify tickets, and choose the car body type in three steps (as shown) when arranging passengers on board. So in an abstract classStationcontains the Security check method (Safetyexamine), verify Ticket method (Validateticket), select the Body type method (Choicecarriagetype) represents an abstract method for a ride step, and the abstract class also containsRidingstep ()method, this method calls the security check method sequentially, validates the ticket method, chooses the car body type method, namely abstract class StationUseRidingstep ()method encapsulates the ride step. Ridingstep ()method is called by theSafetyexamine (),Validateticket (),Choicecarriagetype ()methods are abstract methods, so Stationsub-class:railwaystation(railway station) in the realizationSafetyexamine (),Validateticket (),Choicecarriagetype ()methods, the method of security check, the way of ticket checking and the type of vehicle selected were given respectively. When Stationclass declares a variable that holds its subclassrailwaystationinstance, the variable can call theRidingstep ()method shows the passenger's ride steps. The concrete class diagram looks like this:

Figure One: Ride steps

The template approach is about how to integrate several methods into a single method to form an algorithmic skeleton that solves the problem. The key to the template method pattern is to define an algorithm skeleton in an abstract class, to integrate several methods into a method, and call the method a template method, or simply a template. Other methods called by template methods are often referred to as abstract methods, which are equivalent to the steps in the skeleton of an algorithm that can be done by subclasses.

Structure of the 1.2 pattern

The template method pattern consists of the following two roles:

(1) abstract template: Abstract template is an abstract class. Abstract templates define several methods to represent the steps of an algorithm, in which there are abstract methods and non-abstract methods, in which the abstract method is called the primitive operation (Primitive operation). It is important that an abstract template also defines a method called a template method that contains not only the method calls that represent the algorithm steps in the abstract template, but also the method calls that define the other objects in the abstract template, that is, the template method defines the skeleton of the algorithm.

(2) concrete templates (concrete template): The concrete template is the subclass of the abstract template, which implements the primitive operation in the abstract template.

The class diagram for the template method pattern structure is shown in two:


Figure two: Class diagram of template method pattern

1.3 Template Method advantages of the model

(1) The detailed template implementation details do not alter the framework of the entire algorithm by giving the mature algorithm steps in the abstract template definition template method without limiting the details of the step.

(2) in the abstract template mode, the hook method can be used to hook up some steps, the specific template through the hook can choose some of the steps in the algorithm skeleton.

1.4 Fit Using template Methods pattern of the scene

(1) designers need to give a fixed step of the algorithm, and the specific implementation of some of the steps to leave the subclass to achieve.

(2) The code needs to be refactored toextract the public behavior of each subclass into a common parent class to avoid code duplication.

Design Patterns Learning Notes (18: Template method Mode)

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.