Design Pattern-template method pattern

Source: Internet
Author: User

I. Schema Definition

The template method mode defines the skeleton of an algorithm in a method, and delays some steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm structure.

Ii. embodied Design Principles

Hollywood principle: Do not call (CALL) us. We will call (CALL) You.
This principle requires that only the high-level components (such as the template method of abstractclass in 2) Call the lower-level components (such as the primitiveoperation1 method in concreteclass in 2 ). Low-level components can be linked to high-level components, but they cannot be directly called to prevent high-level components from being mutually dependent.
The observer pattern also embodies this principle.

Iii. UML diagram

Iv. application scenarios

When the algorithm framework has been determined, but small steps have different implementations when not needed, it is best to use the template method mode. For example, a drawing program can draw various straight lines and curves. After the basic path of the line is drawn, the anti-sawtooth processing is required, but some curves need to be anti-sawtooth using method, some curves need to use method B for anti-aliasing. At this time, the basic logic framework of line drawing is defined in the parent class, but the implementation of this small step of anti-aliasing is distributed in each subclass, avoiding code redundancy.

Another more famous example is the Hook Technology in Windows programming, which is the best application of the template method mode. For example, if the user inputs XXX by hanging a monitoring keyboard hook in his program, the corresponding operation will be performed, but be careful not to be killed by 360 as a Trojan.

5. Notes

In reality, there are some special template method patterns. For example, the STD: Sort () method in STL can sort objects in the container, but an imitation function must be provided for comparison between container objects. For example:

Vector <myclass> myvector; Class cmpfunctor {public: bool operator () (const myclass & ob1, const myclass & ob2) {return .... // Compare according to some features of myclass}; STD: Sort (myvector. Begin (), myvector. End (), cmpfunctor );

The STL method sort () itself is an application of the template method mode. The customer provides a comparison tool, namely cmpfunctor. In sort (), the function pointer is used to call this function. Even if there is no inheritance, I think it can be seen as an application of the template method mode. Is it correct ???

Vi. Examples

Reference 4

VII. Sample Code

Wikipedia-http://zh.wikipedia.org/wiki/%E6%A8%A1%E6%9D%BF%E6%96%B9%E6%B3%95

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.