模板方法模式Python版--考题抄错会做也白搭
2015-03-09 16:49
375 查看
重复=易错+难改
提炼代码
改动一下,增加answer
模板方法模式通过把不变行为搬移到超类,去除子类中的重复代码来体现它的优势。
模板方法模式就是提供了一个很好的代码复用平台。
class TestPaperA 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 TestPaperA 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 paper: " studentA = TestPaperA() studentA.TestQuestion1() studentA.TestQuestion2() studentA.TestQuestion3() studentB = TestPaperB() studentB.TestQuestion1() studentB.TestQuestion2() studentB.TestQuestion3()
提炼代码
class TestPaper: def TestQuestion1(self): print "Test question 1" def TestQuestion2(self): print "Test question 2" def TestQuestion3(self): print "Test question 3" class TestPaperA(TestPaper): def TestQuestion1(self): TestPaper.TestQuestion1() print "Answer: b" def TestQuestion2(self): TestPaper.TestQuestion2() print "Answer: a" def TestQuestion3(self): TestPaper.TestQuestion3() print "Answer: c" class TestPaperB(TestPaper): def TestQuestion1(self): TestPaper.TestQuestion1() print "Answer: d" def TestQuestion2(self): TestPaper.TestQuestion2() print "Answer: b" def TestQuestion3(self): TestPaper.TestQuestion3() print "Answer: a"客户端代码同上
改动一下,增加answer
class TestPaper: 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 TestPaperA(TestPaper): def Answer1(self) return "b" def Answer2(self) return "a" def Answer3(self) return "c" class TestPaperB(TestPaper): 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()
模板方法模式通过把不变行为搬移到超类,去除子类中的重复代码来体现它的优势。
模板方法模式就是提供了一个很好的代码复用平台。
相关文章推荐
- 第十章 考题抄错会做也白搭-模板方法模式(读书笔记)
- 《大话设计模式》之--第10章 考题抄错会做也白搭----模板方法模式
- 大话设计模式-第10章 考题抄错会做也白搭--模板方法模式
- 《大话设计模式》--考题抄错会做也白搭--模板方法模式<TemplateMethod>(10)
- 考题抄错会做也白搭—模板方法模式
- 《大话设计模式》—— 读后感 (10)考题抄错会做也白搭——模板方法模式
- 大话设计模式十:模板方法模式(考题抄错会做也白搭)
- 《大话设计模式》之--第10章 考题抄错会做也白搭----模板方法模式
- 第十章 考题抄错会做也白搭—模板方法模式
- 模板方法模式(python)
- 【设计模式】考题 -- 模板方法模式
- 大话设计模式(Python版)--模板方法模式
- python设计模式之模板方法
- 设计模式-模板方法模式-template-python
- 模板方法模式(python)
- 设计模式之python版(七)--模板方法模式
- [python实现设计模式]-2.模板方法模式---把大象关进冰箱.
- Python设计模式(四) -- 模板方法模式
- 【设计模式】考题 -- 模板方法模式
- 简介Python设计模式中的代理模式与模板方法模式编程