模板方法模式(python)
2011-10-11 10:38
363 查看
模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。因此模板方法使得子类可以在不改变一个算法的结构的情况下重新定义该算法的某些特定变量。
优点:把不变行为搬移到超类,去除子类中的重复代码。
类图:
优点:把不变行为搬移到超类,去除子类中的重复代码。
#encoding=utf-8 # #by panda #模板方法模式 def printInfo(info): print unicode(info, 'utf-8').encode('gbk') #金庸小说试题考卷 class TestPaper: def TestQuestion1(self): printInfo('练成倚天剑和屠龙刀的玄铁可能是[] a.球墨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维') printInfo('答案:'+self.Answer1()) def TestQuestion2(self): printInfo('杨过、程英、陆无双铲除了情花,造成[] a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏生态平衡 d.造成该地区沙漠化') printInfo('答案:'+self.Answer2()) def TestQuestion3(self): printInfo('蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[] a.阿司匹林 b. 牛黄解毒片 c.氟哌酸 d.喝大量的生牛奶 e.以上全不对') printInfo('答案:'+self.Answer3()) def Answer1(self): return '' def Answer2(self): return '' def Answer3(self): return '' #学生甲 class TestPaperA(TestPaper): def Answer1(self): return 'A' def Answer2(self): return 'B' def Answer3(self): return 'C' #学生乙 class TestPaperB(TestPaper): def Answer1(self): return 'B' def Answer2(self): return 'D' def Answer3(self): return 'E' def clientUI(): a = TestPaperA(); a.TestQuestion1() a.TestQuestion2() a.TestQuestion3() b = TestPaperB(); b.TestQuestion1() b.TestQuestion2() b.TestQuestion3() return if __name__ == '__main__': clientUI();
类图:
相关文章推荐
- 设计模式-模板方法模式-template-python
- [python实现设计模式]-2.模板方法模式---把大象关进冰箱.
- python设计模式之模板方法模式
- 简介Python设计模式中的代理模式与模板方法模式编程
- Python设计模式(二十三)【模板方法模式】
- 简介Python设计模式中的代理模式与模板方法模式编程
- Python设计模式(四) -- 模板方法模式
- 模板方法模式(python)
- “模板方法”模式与Python装饰器的关系
- 模板方法模式(python)
- 模板方法模式Python版--考题抄错会做也白搭
- 设计模式之python版(七)--模板方法模式
- python设计模式之模板方法
- Python模板方法模式
- 大话设计模式(Python版)--模板方法模式
- 模板方法模式- 设计模式学习
- 《Android源码设计模式》--模板方法模式
- 设计模式-模板方法模式
- Python用list或dict字段模式读取文件的方法
- 设计模式C++实现(5)——原型模式、模板方法模式