[Python设计模式] 第10章 怎么出试卷?——模版方法模式

it2022-05-21  59

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

点评

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

转载于:https://www.cnblogs.com/CheeseZH/p/9404193.html


最新回复(0)