'''
模板方法模式（Template Method）
定义算法的骨架，而将一些步骤延迟到子类中实现。
'''

from abc import ABC, abstractmethod  
  
class AbstractClass(ABC):  
    def template_method(self):  
        # 这是一个模板方法，它定义了一个算法的骨架  
        self.base_operation1()  
        self.required_operations1()  
        self.base_operation2()  
        self.hook1()  
        self.required_operations2()  
        self.base_operation3()  
        self.hook2()  
  
    @abstractmethod  
    def base_operation1(self):  
        pass  
  
    @abstractmethod  
    def base_operation2(self):  
        pass  
  
    @abstractmethod  
    def base_operation3(self):  
        pass  
  
    @abstractmethod  
    def required_operations1(self):  
        pass  
  
    @abstractmethod  
    def required_operations2(self):  
        pass  
  
    def hook1(self):  
        pass  # 钩子操作，子类可以选择是否覆盖  
  
    def hook2(self):  
        pass  # 另一个钩子操作


class ConcreteClass(AbstractClass):  
    def base_operation1(self):  
        print("AbstractClass says: I am doing the bulk of the work")  
  
    def base_operation2(self):  
        print("AbstractClass says: But I let subclasses override some operations")  
  
    def base_operation3(self):  
        print("AbstractClass says: But I am doing the bulk of the work anyway")  
  
    def required_operations1(self):  
        print("ConcreteClass says: Implemented Operation1")  
  
    def required_operations2(self):  
        print("ConcreteClass says: Implemented Operation2")  
  
    def hook1(self):  
        print("ConcreteClass says: Overridden Hook1")  
  
    def hook2(self):  
        # 没有覆盖hook2，所以它将执行AbstractClass中的空实现  
        pass

if __name__ == "__main__":  
    concrete_class = ConcreteClass()  
    concrete_class.template_method()    