Template Method-template method mode

Source: Internet
Author: User
PurposeDefine the calculation skeleton in an operation and delay the implementation of some steps to the subclass, the template method mode allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
CaseAn application framework containing the application and document classes. The application class is responsible for opening an external document. When the content in the document is read, it is represented by document. There is a method OpenDocument in the application to perform document operations:
 
 
  1. void Application::openDocument(const char* name)
  2. {
  3. if(!canOpenDocument(name))
  4. return;
  5. Document* doc = createDocument();
  6. if(doc != NULL)
  7. {
  8. m_docs->addDocument(doc);
  9. aboutToOpenDocument(doc);
  10. doc->open();
  11. doc->read();
  12. }
  13. }
The OpenDocument of application defines every major step for opening a document. It checks whether the document can be opened and creates a Document Object related to the application, and read the document from the file. In this case, OpenDocument is Template Method. A template method defines an algorithm with some abstract operations, and the subclass will redefine these operations to provide specific behavior:

Class application provides operations that require subclass redefinition:
 
 
  1. class Application
  2. {
  3. public:
  4. void openDocument(const char* name);
  5. protected:
  6. virtual void canOpenDocument(const char* name);
  7. virtual void createDocument();
  8. virtual void aboutToOpenDocument();
  9. private:
  10. vector<Document*> m_docs;
  11. };
Myapplication requires the following operations to be customized:
 
 
  1. class MyApplication : public Application
  2. {
  3. protected:
  4. virtual Document* createDocument();
  5. virtual void aboutToOpenDocument();
  6. };
  7. Document* MyApplication::createDocument()
  8. {
  9. return new MyDocument();
  10. }
  11. void aboutToOpenDocument()
  12. {
  13. // initialize doc object.
  14. }
Document and mydocument are similar implementations.
Applicability
  • Defines the immutable part of an algorithm at a time and leaves the variable behavior to the subclass for implementation.
  • Common behaviors in each subclass should be extracted and concentrated in a common parent class to avoid code duplication.
  • Control subclass extension.

Template Method-template method mode

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.