demand:
Students copy and do topics
Initial code
#-*-encoding: utf-8-*-
# 学生 A's test paper
class TestPaperA
def question1
puts ‘Yang Guo got it, and later gave it to Guo Jing, refining Yitian Sword, the black iron of Tu Longdao may be [] a. ductile iron b. tinplate c. high speed alloy steel d. carbon plastic fiber‘
puts ‘Answer: b’
end
def question2
puts' Yang Guo, Cheng Ying, and Lu Wushuang eradicated the love flower, which caused [] a. to make this plant harmless b. to exterminate a rare species c. to destroy the ecological balance of that biosphere d. to cause the area Desertification '
puts ‘Answer: a’
end
def question3
puts' Blue Phoenix causes Huashan master and apprentice, Taogu Liuxian to vomit more than ever, if you are a doctor, what medicine will they prescribe [] a. aspirin b. bezoar detoxification tablets c. fluoric acid d. let them drink a lot of raw milk e. None of the above
puts ‘Answer: c’
end
end
#Student B's test paper
class TestPaperB
def question1
puts ‘Yang Guo got it, and later gave it to Guo Jing, who made it into Tiantianjian, and the dragon iron of Tulongdao may be [] a. ductile iron b. tinplate c. high speed alloy steel d. carbon plastic fiber‘
puts ‘answer: d’
end
def question2
puts' Yang Guo, Cheng Ying, Lu Wushuang eradicated love flowers, causing [] a. making this plant no longer harmful b. extinction of a rare species c. disrupting the ecological balance of that biosphere d. causing the area Desertification '
puts ‘answer: b’
end
def question3
puts' Blue Phoenix causes Huashan master and apprentice, Taogu Liuxian to vomit more than ever, if you are a doctor, what medicine will they prescribe [] a. aspirin b. bezoar detoxification tablets c. fluoric acid d. let them drink a lot of raw milk e. None of the above '
puts ‘Answer: a’
end
end
puts ‘student ’s test paper’
student1 = TestPaperA.new
student1.question1
student1.question2
student1.question3
puts ‘Exam for Student B ’s copy’
student2 = TestPaperB.new
student2.question1
student2.question2
student2.question3
Problems:
The code in TestPaperA and TestPaperB have many similarities, which is not conducive to maintenance. If you need to modify the problem, you must change two
The changed code
#-*-encoding: utf-8-*-
class TestPaper
def question1
puts ‘Yang Guo got it, and later gave it to Guo Jing, who made it into Tiantianjian, and the dragon iron of Tulongdao may be [] a. ductile iron b. tinplate c. high speed alloy steel d. carbon plastic fiber‘
end
def question2
puts' Yang Guo, Cheng Ying, Lu Wushuang eradicated love flowers, causing [] a. making this plant no longer harmful b. extinction of a rare species c. disrupting the ecological balance of that biosphere d. causing the area Desertification '
end
def question3
Puts' The blue phoenix caused Huashan mentoring and Taogu Liuxian to vomit more than once. If you are a doctor, what medicine will you give them? e. None of the above '
end
end
# 学生 甲 的 Test paper class
class TestPaperA <TestPaper
def question1
super
puts ‘Answer: b’
end
def question2
super
puts ‘Answer: a’
end
def question3
super
puts ‘answer: c’
end
end
# 学生 乙 的 Test paper class
class TestPaperB <TestPaper
def question1
super
puts ‘Answer: d’
end
def question2
super
puts ‘answer: b’
end
def question3
super
puts ‘Answer: a’
end
end
puts ‘student ’s test paper’
student1 = TestPaperA.new
student1.question1
student1.question2
student1.question3
puts ‘Exam for Student B ’s copy’
student2 = TestPaperB.new
student2.question1
student2.question2
student2.question3
It can be seen that a public test paper class is extracted, and A and B are inherited, and the test questions are shared. Then look at TestPaperA and TestPaperB. The only difference is that the answers a, b, c, and d are different, and the others are the same.
#-*-encoding: utf-8-*-
class TestPaper
def question1
puts ‘Yang Guo got it, and later gave it to Guo Jing, who made it into Tiantianjian, and the dragon iron of Tulongdao may be [] a. ductile iron b. tinplate c. high speed alloy steel d. carbon plastic fiber‘
puts "Answer: # {answer1}"
end
def question2
puts' Yang Guo, Cheng Ying, Lu Wushuang eradicated love flowers, causing [] a. making this plant no longer harmful b. extinction of a rare species c. disrupting the ecological balance of that biosphere d. causing the area Desertification '
puts "Answer: # {answer2}"
end
def question3
puts' Blue Phoenix causes Huashan master and apprentice, Taogu Liuxian to vomit more than ever, if you are a doctor, what medicine will they prescribe [] a. aspirin b. bezoar detoxification tablets c. fluoric acid d. let them drink a lot of raw milk e. None of the above
puts "Answer: # {answer3}"
end
def answer1; end
def answer2; end
def answer3; end
end
# 学生 A's test paper
class TestPaperA <TestPaper
def answer1
‘B’
end
def answer2
‘A’
end
def answer3
‘C’
end
end
# 学生 乙 的 Test paper class
class TestPaperB <TestPaper
def answer1
‘D’
end
def answer2
‘B’
end
def answer3
‘A’
end
end
puts ‘student ’s test paper’
student1 = TestPaperA.new
student1.question1
student1.question2
student1.question3
puts ‘Exam for Student B ’s copy’
student2 = TestPaperB.new
student2.question1
student2.question2
student2.question3
Here, the answers in TestPaperA and TestPaperB are separated into the parent class, and only the different parts are saved.
The parent class becomes a template for the child class, and all duplicate code should be raised to the parent class, rather than having each child class repeat.
When we want to complete a process or a series of steps that are consistent at a certain level of detail, but the implementation of individual steps at a more detailed level may be different, we usually consider using the template method pattern to deal with.
"Dahua design pattern" Ruby version code: template method pattern