# -*- coding:UTF-8 -*- import abc class AbstractClass(metaclass=abc.ABCMeta): @abc.abstractmethod def primitive_operation1(self): pass @abc.abstractmethod def primitive_operation2(self): pass def template_method(self): self.primitive_operation1() self.primitive_operation2() print("") class ConcreteClassA(AbstractClass): def primitive_operation1(self): print("具体A方法1实现") def primitive_operation2(self): print("具体A方法2实现") class ConcreteClassB(AbstractClass): def primitive_operation1(self): print("具体B方法1实现") def primitive_operation2(self): print("具体B方法2实现") if __name__=="__main__": c = ConcreteClassA() c.template_method() c=ConcreteClassB() c.template_method()