Template Method pattern defines method in a superclass, usually an abstract superclass, and defines the skeleton of an operation and allows subclasses to override (hooks) specific steps of the algorithm without changing its structure.
This pattern is used when:
The pattern operates with two (or more) entities:
from abc import ABC, abstractmethod
class AbstractRunner(ABC):
def start(self):
print("starting")
@abstractmethod
def handle(self):
...
def finish(self):
print("finishing")
def run(self):
self.start()
self.handle()
self.finish()
class SomeImplementation(AbstractRunner):
def handle(self):
print("handling something")
class AnotherImplementation(AbstractRunner):
def handle(self):
print("another handling implementation")
def main():
impl = SomeImplementation()
impl.run()
impl = AnotherImplementation()
impl.run()
if __name__ == "__main__":
main()