Constitutes 3 Methods
- Abstract - Must be implemented by subclasses
- Concrete - Common enough, that subclass will be okay using the default implementation, can be overriden
- Hooks - Do nothing, but can be overriden to do something
from abc import ABCMeta, abstractmethod
class ITransport(metaclass=ABCMeta):
def __init__(self, destination):
self._destination = destination
def take_trip(self): # Template Method
self.start_engine()
self.leave_terminal()
self.entertainment()
self.travel_to_destination()
self.arrive_at_destination()
@abstractmethod
def start_engine(self): # Abstract, must be implemented in subclass
pass
def leave_terminal(self): # Concrete, but overridable
print("Leaving Terminal")
@abstractmethod
def travel_to_destination(self): # Abstract, must be implemented in subclass
print("Travelling ...")
def entertainment(self): # Hook, concrete but has no implementation
pass
def arrive_at_destination(self): # Concrete, but overridable
print(f"Arriving at {self._destination}")
class Airplane(ITransport):
def start_engine(self):
print("Starting the Rolls-Royce Gas-Turbine Engines")
def leave_terminal(self):
print("Leaving Terminal")
print("Taxiing the Runway")
def travel_to_destination(self):
print("Flying ...")
def entertainment(self):
print("Playing in-flight movie")
def arrive_at_destination(self):
print(f"Landing at {self._destination}")
class Bus(ITransport):
def start_engine(self):
print("Starting the Cummins Diesel Engine")
def travel_to_destination(self):
print("Driving ...")
def travel(destination, transport):
print(f"\n=============== Taking the {transport.__name__} to {destination} ===============\n")
transport(destination).take_trip()
travel('New York', Bus)
travel('Amsterdam', Airplane)
=============== Taking the Bus to New York =============== Starting the Cummins Diesel Engine Leaving Terminal Driving ... Arriving at New York =============== Taking the Airplane to Amsterdam =============== Starting the Rolls-Royce Gas-Turbine Engines Leaving Terminal Taxiing the Runway Playing in-flight movie Flying ... Landing at Amsterdam