Three minutes to understand "template method mode"--easy to master in design mode

Source: Internet
Author: User

The official definition of the template method pattern:

In the template method pattern, only the skeleton of an algorithm is defined, and some steps are deferred to the subclass. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.


Plain English Speaking:

While the process of a function can be determined, some specific steps will vary depending on the situation. In this case, you can use the template method pattern to write out the parts of the function that can be determined, and the indeterminate parts are replaced with the abstract functions in this class; When you need to use this function, you need to create a subclass that implements all of the abstract functions in that class, and when the algorithm is called through the subclass, when it executes to the abstract , the system automatically calls the already rewritten function in the subclass, and the entire algorithm is executed once.


Class diagram for template methods:


In class AbstractClass, the template function is Templatemethod written, and the indeterminate steps in the template function are replaced by abstract functions; When the client wants to use this template function, it needs to create a subclass and override the abstract function of the abstract class.


When to use?

When a similar function is used in more than one place, it is common practice to copy the code and change some places slightly. The reuse rate for this practice code is too low.

The right thing to do: encapsulate functionally similar parts into a function, where you can determine where to write the code directly, instead of using abstract functions in an indeterminate place.


Advantages of the template method pattern:

When immutable and mutable code is mixed together, if you need to use this code in multiple places, you need to copy and paste it, and then modify it slightly, which will cause the invariant code to recur, resulting in a low code reuse rate. The template method pattern is to move the invariant behavior into the parent class, removing duplicate code from the subclass, thus increasing the code reuse rate.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three minutes to understand "template method mode"--easy to master in design 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.