Template method of design pattern

Source: Internet
Author: User

Template method

1. Intent to define the skeleton of an algorithm in an operation, delay some steps into subclasses, and Template method allows subclasses to redefine some steps of the algorithm without altering the structure of an algorithm. 2. Motivation takes into account an application framework that provides application and document classes. The application class is responsible for opening a document that already has an external storage form, such as a file.   Once a file's information is read from the file, it is represented by a Document object. We call the OpenDocument method a template method, a template method that defines an algorithm with an abstract operation, and subclasses redefine these operations to provide specific behavior. The subclasses of application will define specific algorithmic steps to check whether a document can be opened (canopendocument) and create a document (Docreatedocument). The document subclass mydocument defines the algorithm steps (Doread) for reading the documents. By using abstract operations to define one or more of the steps in the algorithm, the template method determines their sequencing, but it allows the application and document subclasses to change these steps to meet their needs. 3. Applicability one-time implementation of the invariant part of the algorithm, the variable part to the subclass implementation of the common behavior of each subclass should be extracted and centralized into the parent class to avoid the extension of the code repeating subclass by the parent class control 4. Structure 5. Collaboration Concreteclass by AbstractClass to achieve the same algorithm in step 6. The effect template method is a basic technique of code reuse, which is especially important in the class library, which provides the public behavior of the class library. The template method leads to a reverse control structure, the "Hollywood Rule", "Don't look for us, we're looking for you", referring to the action of the parent class calling the subclass, not the other way around. 7. Practice very common design patterns, through the parent class definition framework, subclasses to populate

Template method of design 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.