模板方法模式Python版 考題抄錯會做也白搭

2021-06-29 00:42:00 字數 3595 閱讀 5057

重複=易錯+難改

class test*****a

def testquestion1(self):

print "test question 1"

print "answer: b"

def testquestion2(self):

print "test question 2"

print "answer: a"

def testquestion3(self):

print "test question 3"

print "answer: c"

class test*****a

def testquestion1(self):

print "test question 1"

print "answer: d"

def testquestion2(self):

print "test question 2"

print "answer: b"

def testquestion3(self):

print "test question 3"

print "answer: a"

if __name__ == "__main__":

print "student a's test *****: "

studenta = test*****a()

studenta.testquestion1()

studenta.testquestion2()

studenta.testquestion3()

studentb = test*****b()

studentb.testquestion1()

studentb.testquestion2()

studentb.testquestion3()

提煉**

class test*****:

def testquestion1(self):

print "test question 1"

def testquestion2(self):

print "test question 2"

def testquestion3(self):

print "test question 3"

class test*****a(test*****):

def testquestion1(self):

test*****.testquestion1()

print "answer: b"

def testquestion2(self):

test*****.testquestion2()

print "answer: a"

def testquestion3(self):

test*****.testquestion3()

print "answer: c"

class test*****b(test*****):

def testquestion1(self):

test*****.testquestion1()

print "answer: d"

def testquestion2(self):

test*****.testquestion2()

print "answer: b"

def testquestion3(self):

test*****.testquestion3()

print "answer: a"

客戶端**同上

改動一下,增加answer

class test*****:

def testquestion1(self):

print "test question 1"

print "answer: ",self.answer1()

def testquestion2(self):

print "test question 2"

print "answer: ",self.answer2()

def testquestion3(self):

print "test question 3"

print "answer: ",self.answer3()

def answer1(self)

pass

def answer2(self)

pass

def answer3(self)

pass

class test*****a(test*****):

def answer1(self)

return "b"

def answer2(self)

return "a"

def answer3(self)

return "c"

class test*****b(test*****):

def answer1(self)

return "d"

def answer2(self)

return "b"

def answer3(self)

return "a"

模板方法模式

class abstractclass:

def primitiveoperation1(self):

pass

def primitiveoperation2(self):

pass

def templatemethod(self):

self.primitiveoperation1()

self.primitiveoperation2()

class concreteclassa(abstractclass):

def primitiveoperation1(self):

print "concrete class a operation 1"

def primitiveoperation2(self):

print "concrete class a operation 2"

class concreteclassb(abstractclass):

def primitiveoperation1(self):

print "concrete class b operation 1"

def primitiveoperation2(self):

print "concrete class b operation 2"

if __name__ =="__main__":

c = concreteclassa()

c.templatemethod()

c = concreteclassb()

c.templatemethod()

模板方法模式通過把不變行為搬移到超類,去除子類中的重複**來體現它的優勢。

模板方法模式就是提供了乙個很好的**復用平台。

設計模式 考題 模板方法模式

一,概述 模板方法模式 定義乙個操作中的演算法的骨架,而將一些步驟延遲到子類中。模板方法使得子類可以不改變乙個演算法的結構既可以重定義該演算法的某些特定步驟 核心思想 抽象類的方法推遲到子類實現。基類示例指向子類實現 二,示例 老師出考題,學生抄題,然後答題。1 第一種實現方式 缺點 學生每次都要抄...

模板方法模式 考題抄錯會做也白搭

模板方法 當我們要完成在某一細節層次一致的乙個過程或一系列步驟,但其個別步驟在更詳細的層次上的實現可能不同時,我們通常考慮用模板方法模式來處理。模板方法模式 定義乙個操作中的演算法的骨架,而將一些步驟延遲到子類中。模板方法使得子類可以不改變乙個演算法的結構即可重定義該演算法的某些特定步驟。模板方法模...

考題抄錯會做也白搭 模板方法模式

10.1 考題不會做,蒙唄!10.2 重複 易做 難改 namespace 模板方法模式 學生甲抄的試卷 class test a 試題2 public void testquestion2 試題3 public void testquestion3 學生乙抄的試卷 class test b 試題2...