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 ()}