Design Pattern _ Template Method

Source: Internet
Author: User

Image example:

Have you read the classic article "how to persuade girls to go to bed? From the process of realizing that a girl goes to bed, there are eight steps (Template Method): encounter, break the deadlock, pursue, kiss, foreplay, start-up, caress, and enter ), however, each step has a different approach for different situations. It depends on your response (specific implementation );

Template Method mode:

The template method mode prepares an abstract class, implements some logic in the form of a specific method and a specific constructor, and then declares some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. First, create a top-level logic framework, and leave the logic details to specific sub-classes for implementation.

Purpose:
Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Templatemethod allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

UML structure diagram:


Abstract base class:
1) abstractclass: abstract base class, defining the outline of an algorithm

Resolution:
The key to templatemethod is to define an algorithm profile in the base class, but the specific implementation of each step of the algorithm is left to the derived class. however, this will also cause the design to be less flexible, because it is difficult to change the outline, which is also the reason for the priority of aggregation rather than inheritance.

 

Design Pattern _ Template Method

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.