Template Method mode (Python)

Source: Internet
Author: User

Template Method Mode: Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Therefore, the template method allows subclass to redefine certain variables of an algorithm without changing the structure of an algorithm.

Advantage: Moving the unchanged behavior to the superclass removes repeated code in the subclass.

# Encoding = UTF-8 ## by Panda # template method mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # class testpaper: def testquestion1 (Self): printinfo. ductile Iron B. tinplate C. high-speed alloy steel D. carbon fiber ') printinfo ('answer:' + self. answer1 () def testquestion2 (Self): printinfo. make this plant no longer harm B. extinction of a rare species C. damage the ecological balance D. cause desertification in the region ') printinfo ('answer:' + self. answer2 () def testquestion3 (Self): printinfo. aspirin B. niuhuang Jiedu tablets C. d. drinking a lot of raw milk E. none of the above) printinfo ('answer: '+ self. answer3 () def answer1 (Self): Return ''def answer2 (Self): Return ''def answer3 (Self): Return ''# student a class testpapera (testpaper ): def answer1 (Self): Return 'A' def answer2 (Self): Return 'B' def answer3 (Self): Return 'C' # student B class testpaperb (testpaper ): def answer1 (Self): Return 'B' def answer2 (Self): Return 'D' def answer3 (Self): Return 'E' def clientui (): A = testpapera ();. testquestion1 (). testquestion2 (). testquestion3 () B = testpaperb (); B. testquestion1 () B. testquestion2 () B. testquestion3 () returnif _ name _ = '_ main _': clientui ();

Class diagram:

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.