Template method pattern for design pattern (behavioral type)

Source: Internet
Author: User

PS One sentence: Eventually choose Csdn to organize the publication of the knowledge points of these years, the article parallel migration to CSDN. Because CSDN also support markdown grammar, Ah!

"Craftsman Joshui Http://blog.csdn.net/yanbober" read the previous "command pattern" of the design pattern (behavioral type) http://blog.csdn.net/yanbober/article/ details/45500113

Overview

Template method pattern is a kind of code reuse based on inheritance, it is a kind of behavior pattern, it is the simplest type of behavioral design pattern, and there is only inheritance relationship between parent class and subclass in its structure. By using the template method pattern, you can encapsulate the implementation steps of some complex process in a series of basic methods, provide a method called template method in the abstract parent class to define the order of execution of these basic methods, and override some steps by its subclasses, so that the same algorithm framework can have different execution results. The template method pattern provides a template method to define the algorithm framework, and the implementation of some specific steps can be done in its subclasses.

Core

Concept: defines the framework for an algorithm in an operation, and delays some steps into subclasses. The template method pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

Template method pattern structure important Core module:

AbstractClass (abstract Class)

A series of basic operations (primitiveoperations) are defined in an abstract class, which can be concrete or abstract, and each basic operation corresponds to a step in the algorithm that can be redefined or implemented in its subclasses. At the same time, a template method is implemented in an abstract class to define the framework of an algorithm, which can invoke not only the basic methods implemented in the abstract class, but also the basic methods implemented in subclasses of the abstract class, and can also invoke methods in other objects. So the abstraction layer in the template method pattern can only be an abstract class, not an interface.

Concreteclass (Specific sub-class)

It is a subclass of an abstract class that implements the steps of an abstract basic operation declared in a parent class to complete a subclass-specific algorithm, or overrides a specific basic operation already implemented in the parent class.

Usage Scenarios

Some complex algorithms are segmented, and the invariant parts of the algorithm are designed as template methods and parent class concrete methods, and some of the details can be changed by their subclasses.

The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication.

Subclasses are required to determine whether a step in the parent class algorithm executes, implementing the subclass's reverse control of the parent class.

Program Ape Instance

Here is a basic simple example of a template method pattern, not many explanations:

 PackageYanbober.github.io;//abstractclass (abstract class)AbstractClass AbstractClass {protected Abstract void Absmethod();protected void Hookmethod() {//base NULL}Private Final void Concretemethod() {System.out.println ("Base Logic code!"); } Public void Templatemethod() {Absmethod ();        Hookmethod ();    Concretemethod (); }}//concreteclass (Specific sub-class)Class Concreteclass extends AbstractClass {@Override    protected void Absmethod() {System.out.println ("Signal Logic code!"); }@Override    protected void Hookmethod() {Super. Hookmethod (); System.out.println ("Hookmethod Logic code!"); }}//Client Public  class Main {     Public Static void Main(string[] args) {AbstractClass AbstractClass =NewConcreteclass ();    Abstractclass.templatemethod (); }}
sum up a

Template Method Pattern Advantages:

A formal definition of an algorithm in the parent class, with its subclasses implementing the details, does not alter the order of execution of the steps in the algorithm when the subclass implements the detailed processing algorithm.

The template method pattern is a code reuse technique, which is particularly important in the class library design, which extracts the public behavior in the class library, puts the public behavior in the parent class, and implements different behaviors through its subclasses, which encourages us to use inheritance appropriately to implement code reuse.

A reverse control structure can be implemented to determine whether a particular step needs to be performed by overriding the parent class's Hook method.

The basic method of the parent class can be overridden by subclasses in the template method pattern, and different subclasses can provide different implementations of the basic methods, and it is convenient to replace and add new subclasses, which conform to the single duty principle and the open and closed principle.

Template Method Pattern Disadvantages:

It is necessary to provide a subclass for the different implementations of each of the basic methods, and if there are too many variables in the parent class, it will result in an increase in the number of classes, a larger system, and a more abstract design, which can be designed in conjunction with bridging mode.

"Craftsman Joshui Http://blog.csdn.net/yanbober" continue to read the mode of design (behavioral) in state pattern http://blog.csdn.net/yanbober/article/ details/45502665

Template method pattern for design pattern (behavioral type)

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.