github地址:https://github.com/cheesezh/python_design_patterns

题目

小时候数学老师的随堂测验,都是老师在黑板上写题目,学生在下边抄,然后再做题目。设计一个程序,模拟学生A和B抄题目做试卷的过程。

基础版本


class TestPaperA(): def test_question_1(self):
print("题目1: !+1=?,a.2 b.3 c.4. d.1")
print("我选:a") def test_question_2(self):
print("题目2: 2+1=?,a.2 b.3 c.4. d.1")
print("我选:b") def test_question_3(self):
print("题目3: 2+2=?,a.2 b.3 c.4. d.1")
print("我选:c") class TestPaperB(): def test_question_1(self):
print("题目1: !+1=?,a.2 b.3 c.4. d.1")
print("我选:a") def test_question_2(self):
print("题目2: 2+1=?,a.2 b.3 c.4. d.1")
print("我选:c") def test_question_3(self):
print("题目3: 2+2=?,a.2 b.3 c.4. d.1")
print("我选:d") def main():
print("学生A抄的试卷以及答案")
paper_a = TestPaperA()
paper_a.test_question_1()
paper_a.test_question_2()
paper_a.test_question_3()
print("学生B抄的试卷以及答案")
paper_b = TestPaperB()
paper_b.test_question_1()
paper_b.test_question_2()
paper_b.test_question_3() main()
学生A抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:b
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:c
学生B抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:c
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:d

点评

  • 学生A和学生B的考卷题目完全一样,重复代码太多
  • 如果老师修改题目,那所有学生都需要改试卷
  • 把试卷和答案分离,抽象一个试卷父类,然后学生A和学生B的试卷继承这个父类即可

改进版本1.0——提炼父类

class TestPaper():

    def test_question_1(self):
print("题目1: !+1=?,a.2 b.3 c.4. d.1") def test_question_2(self):
print("题目2: 2+1=?,a.2 b.3 c.4. d.1") def test_question_3(self):
print("题目3: 2+2=?,a.2 b.3 c.4. d.1") class TestPaperA(TestPaper): def test_question_1(self):
super().test_question_1()
print("我选:a") def test_question_2(self):
super().test_question_2()
print("我选:b") def test_question_3(self):
super().test_question_3()
print("我选:c") class TestPaperB(TestPaper): def test_question_1(self):
super().test_question_1()
print("我选:a") def test_question_2(self):
super().test_question_2()
print("我选:c") def test_question_3(self):
super().test_question_3()
print("我选:d") main()
学生A抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:b
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:c
学生B抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:c
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:d

点评

这还是只初步的泛化,两个类中还有类似的代码。比如都有super().test_question_1(),还有print("我选:a"),除了选项不同,其他都相同。

我们既然用了继承,并且认为这个继承是有意义的,那么父类就应该成为子类的模版,所有重复的代码都应该要上升到父类去,而不是让每个子类都去重复。

这就需要使用模版方法来处理。

当我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模版方法来处理。

改进版本2.0——提炼细节

from abc import ABCMeta, abstractmethod

class TestPaper():

    __metaclass__ = ABCMeta

    def test_question_1(self):
print("题目1: !+1=?,a.2 b.3 c.4. d.1")
print("我选:{}".format(self.answer_1())) @abstractmethod
def answer_1(self):
pass def test_question_2(self):
print("题目2: 2+1=?,a.2 b.3 c.4. d.1")
print("我选:{}".format(self.answer_2())) @abstractmethod
def answer_2(self):
pass def test_question_3(self):
print("题目3: 2+2=?,a.2 b.3 c.4. d.1")
print("我选:{}".format(self.answer_3())) @abstractmethod
def answer_3(self):
pass class TestPaperA(TestPaper): def answer_1(self):
return "a" def answer_2(self):
return "b" def answer_3(self):
return "c" class TestPaperB(TestPaper): def answer_1(self):
return "a" def answer_2(self):
return "c" def answer_3(self):
return "d" main()
学生A抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:b
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:c
学生B抄的试卷以及答案
题目1: !+1=?,a.2 b.3 c.4. d.1
我选:a
题目2: 2+1=?,a.2 b.3 c.4. d.1
我选:c
题目3: 2+2=?,a.2 b.3 c.4. d.1
我选:d

点评

此时要有更多的学生来答试卷,只是在试卷的模版上填写选择题的选项答案,即可。

模版方法

模版方法,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤[DP]。

from abc import ABCMeta, abstractmethod

class AbstractClass():
"""
抽象模版类,定义并实现了一个模版方法,这个模版方法一般是一个具体的算法,
它定义了一个顶级逻辑的骨架,而逻辑的组成步骤在相应的抽象操作中,推迟到
子类实现。当然,顶级逻辑也可能调用一些具体方法。
"""
__metaclass__ = ABCMeta @abstractmethod
def primitive_operation_1(self):
"""
抽象操作1,放到子类去实现
"""
pass @abstractmethod
def primitive_operation_2(self):
"""
抽象操作2,放到子类去实现
"""
pass def template_method(self):
"""
具体模版方法,定义了顶级逻辑骨架
"""
self.primitive_operation_1()
self.primitive_operation_2() class ConcreteClassA(AbstractClass):
"""
具体类A,给出抽象方法的不同实现
"""
def primitive_operation_1(self):
print("具体类A的操作1") def primitive_operation_2(self):
print("具体类A的操作2") class ConcreteClassB(AbstractClass):
"""
具体类B,给出抽象方法的不同实现
"""
def primitive_operation_1(self):
print("具体类B的操作1") def primitive_operation_2(self):
print("具体类B的操作2") cls = ConcreteClassA()
cls.template_method() cls = ConcreteClassB()
cls.template_method()
具体类A的操作1
具体类A的操作2
具体类B的操作1
具体类B的操作2

点评

  • 模版方法通过把不变的行为搬移到超类,去除子类中的重复代码来体现它的优势
  • 模版方法提供了一个很好的代码复用平台
  • 当不变的和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。我们通过模版方法模式把这些行为搬移到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠

[Python设计模式] 第10章 怎么出试卷?——模版方法模式的更多相关文章

  1. [Python设计模式] 第12章 基金理财更省事——外观模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目1 用程序模拟股民直接炒股的代码,比如股民投资了股票1,股票2,股票3,国债 ...

  2. [Python设计模式] 第22章 手机型号&软件版本——桥接模式

    github地址:https://github.com/cheesezh/python_design_patterns 紧耦合程序演化 题目1 编程模拟以下情景,有一个N品牌手机,在上边玩一个小游戏. ...

  3. [Python设计模式] 第2章 商场收银软件——策略模式

    github地址: https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序, 模拟商场收银软件,根据客户购买商品的单价和数量,计 ...

  4. [Python设计模式] 第25章 联合国维护世界和平——中介者模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目背景 联合国在世界上就是中介者的角色,各国之间的关系复杂,类似不同的对象和对 ...

  5. [Python设计模式] 第23章 烤串的哲学——命令模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目1 用程序模拟,顾客直接向烤串师傅提需求. class Barbecuer( ...

  6. JS常用的设计模式(10)——模版方法模式

    模式方法是预先定义一组算法,先把算法的不变部分抽象到父类,再将另外一些可变的步骤延迟到子类去实现.听起来有点像工厂模式( 非前面说过的简单工厂模式 ). 最大的区别是,工厂模式的意图是根据子类的实现最 ...

  7. Python设计模式——模版方法模式

    1.模版方法模式 做题的列子: 需求:有两个学生,要回答问题,写出自己的答案 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Stude ...

  8. 【java设计模式】(10)---模版方法模式(案例解析)

    一.概念 1.概念 模板方法模式是一种基于继承的代码复用技术,它是一种类行为型模式. 它定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的 ...

  9. 第13章 模版方法模式(Template Method)

    原文  第13章 模版方法模式(Template Method) 模板模式 模板模式 举例:模拟下数据库的update方法,先删除在插入. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

随机推荐

  1. pytest五:fixture_autouse=True

    平常写自动化用例会写一些前置的 fixture 操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传返个参数,会比较麻烦.fixture 里面有个参数 autouse,默讣是 ...

  2. intellij idea svn 修改文件后,父文件夹也标注修改

    svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File—->settings—->version control—–>勾选show ...

  3. Ckeditor一种很方便的文本编辑器

    ckeditor官网:http://ckeditor.com/ 这里介绍ckeditor的其中一个的用法,自己做小项目练手非常的适合,上手非常的快. 首先去官网下载这个东西,链接:http://pan ...

  4. Codeforces Round #467 (Div. 2) E -Lock Puzzle

    Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...

  5. 转载收藏用<meta name="ROBOTS"

    SEO优化meta标签 name="robots" content="index,follow,noodp,noydir"解释 (2012-10-11 10:33:08)转载   SEO优化meta标 ...

  6. setting.xml配置文件

    在此,简单的说下.  setting.xml 和 pom.xml这两各配置文件,到此是怎样? setting.xml setting.xml,这个配文件,是全局的. 比如你的是构建,web项目.我的是 ...

  7. MySQL QA

    Q:MySQL常用的存储引擎有哪些? A:MyISAM及InnoDB,5.5版本后默认数据库引擎由MyISAM变为InnoDB Q:MyISAM及InnoDB有什么区别?至少5点 A: ①.InnoD ...

  8. chrome浏览器调试工具你会使用吗?

    浏览器调试工具使用总结 一. console使用 console.table(),可以把对象输出成表格的形式,直观的观察数据. console.dir(),可以直观观察dom元素的对象形式 二. $的 ...

  9. Docker镜像优化

    前言 上篇博文说到使用Visual Studio Tools for Docker帮助我们生成Dockerfile,现在我们讨论下生成的Dockerfile的优劣. 一.以往Dockerfile构建模 ...

  10. 移动端Tap与滑屏实战技巧总结以及Vue混合开发自定义指令

    最近在忙混合开发,因交互相对复杂,所以也踩了很多坑.在此做一下总结. 1.tap事件的实际应用 在使用tap事件时,老生常谈的肯定是点透问题,大多情况下,在有滑屏交互的页面时,我们会在根节点阻止默认行 ...