Java design Pattern----Template Method pattern

Source: Internet
Author: User

Template method Mode:

The skeleton of an algorithm is defined in a method, and some steps are deferred to subclasses. The template method allows subclasses to redefine some of the implementations in the algorithm without changing the structure of the algorithm.

Hollywood principles:

Don't call (call) us, we'll call (call) you.

Points:

1, "Template Method" defines the steps of the algorithm, the implementation of these steps to defer to the subclass

2. Template method mode provides us with an important technique for code reuse.

3. Abstract classes of template methods can define concrete methods, abstract methods, and hooks

4. The abstract method is implemented by subclasses

5, Hook is a method, it does not work in the abstract class, or only do the default things, subclasses can choose to do not overwrite it

6, in order to prevent subclasses from changing the algorithm in the template method, the template method can be declared as final

7. Hollywood principles tell us to put decision-making right in a high-level module in order to decide how to call the underlying module in time

8. You will see many variants of template method patterns in real-world code, and don't expect them to be recognized by you at a glance.

9, the Strategy mode and the template method pattern all encapsulate the algorithm, one uses the combination, one uses the inheritance

10, engineering method is a special version of the template method

Java design Pattern----Template Method pattern

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.