Original design mode (Golang) Seven, template method mode

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Pattern Features: Move to the parent class with unchanging behavior, removing duplicate code from subclasses.
Program Example: Use the same test paper (parent class), different students submit their own papers (subclass method implementation)

Package Mainimport ("FMT") type testpaper struct {child interface{}}func (t *testpaper) TestQuestion1 () {fmt. Println ("Yang too Get, later gave Guo Jing, practice into the sword, Dragon Sword of the Xuan Iron may be [] a. Ball mill cast iron B. tinplate c. High-speed alloy key D. Carbon fiber") fmt. Println ("Answer:", T.child. ( Answers). Answer1 ())}func (t *testpaper) TestQuestion2 () {fmt. Println ("Yang, Chengying, Lu Matchless to eradicate the feeling of flowers." caused [] a. To cause the plant to no longer harm B. Extinction of a rare species C. destruction of the ecological balance of the biosphere D. "Desertification in the region") fmt. Println ("Answer:", T.child. ( Answers). Answer2 ())}func (t *testpaper) TestQuestion3 () {fmt. Println ("Blue Phoenix caused the Huashan apprentice, Peach Valley six cents vomit not only, if you are a doctor, will give them what medicine [] a. Aspirin b. Bezoar jiedu tablets c. Fluoride acid d. Let them drink a lot of raw milk e. None of the above") FMT. Println ("Answer:", T.child. ( Answers). Answer3 ())}type Answers Interface {answer1 () Stringanswer2 () Stringanswer3 () String}type testpapera struct { Testpaper}func Newtestpapera () *testpaper {paper: = &testpaper{}var Answer Answers = &testpapera{}paper.child = A  Nswerreturn Paper}func (t *testpapera) Answer1 () string {return "B"}func (t *testpapera) Answer2 () string {return "C"}func (t *testpapera) Answer3 () string {return "a"}type testpaperb struct {testpaper}func NewTestpaperb () *testpaper {paper: = &testpaper{}var Answer Answers = &testpaperb{}paper.child = Answerreturn paper} Func (t *testpaperb) Answer1 () string {return "C"}func (t *testpaperb) Answer2 () string {return "a"}func (t *testpaperb) a Nswer3 () string {return "a"}func main () {studenta: = Newtestpapera () studenta.testquestion1 () Studenta.testquestion2 () Studenta.testquestion3 () Studentb: = Newtestpaperb () studentb.testquestion1 () Studentb.testquestion2 () Studentb.testquestion3 ()}

 

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.