Template design-one of the behavior design patterns

Source: Internet
Author: User

1. The template method defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. Features: when using the template method, you can move the unchanged behavior to the superclass, removing repeated code in the subclass to reflect its advantages. It provides a good code reuse platform. Applicability: When immutable and variable behaviors are mixed in the implementation of sub-classes of methods, immutable behaviors will be repeated in sub-classes. At this time, we can consider using the template method to move these actions to a single place, so as to help the subclass get rid of the entanglement of repeated unchanged behavior. Example, the class has to be modified. Not only do they have to be modified, but some are nearsighted and often make mistakes, which leads to incorrect answers. Students answer questions one by one. Later, the school had money and bought a printing press. The teacher engraved the template. After the correction, he copied the paper. In this way, the students saved the trouble of copying the questions. At the same time, everyone had the same paper, there will be no more CC errors. Students only need to fill in the correct answer and submit the paper. The distribution of this exam is first carried out by the teacher to engrave the template, print the paper, and leave the answer to the students. What are the template methods? II. In the template method, the class chart generally contains the abstract method PrimitiveOperation in the TemplateMethod method of the parent class, while the PrimitiveOperation method is implemented by polymorphism in the subclass, this completes the partial redefinition of the algorithm.

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.