I have a text file which is evolving and thus, difficult to manage. Hence, I am going to rewrite the code from scratch. Since, only few parts of the txt file change with a newer version, from the answers here, I understand that I should prefer a template design pattern rather than a strategy design pattern.
Below is an example of my text files,
version 1
carname = a
cartype = sedan
wheel = 4
carengine = diesel
version 2
carname = b
cartype = sedan
wheel = 4
carengine = petrol, enginetype = turbo
gearbox = manual
How can I use a template design pattern when my template changes (in this case the gearbox)? Should I have to modify the template class? If yes, how to modify the V1 concrete class?
Below is a sample code,
class TemplateClass():
_name: str
_type: str
_wheel: float
_engine: CarEngineTemplatePattern
def template_method(self) -> None:
self.get_car_basic_details()
self.get_car_type()
self.get_wheel()
self.get_car_engine()
def get_car_basic_details(self) -> None:
# Reads the car name and stores it
self._name = # readline from the text file
def get_car_type(self) -> None:
# Reads the car type and stores it
self._type = # readline from the text file
def get_wheel(self) -> None:
# Reads the car wheel count and stores it
self._wheel = # readline from the text file
@abstractmethod
def get_car_engine(self) -> None:
pass
class V1(TemplateClass):
def get_car_engine(self) -> None:
# Reads the car engine and stores it
engine = CarEngineTemplatePatternV1("diesel")
self._engine = engine
class V2(TemplateClass):
def get_car_engine(self) -> None:
# Reads the car engine and stores it
engine = CarEngineTemplatePatternV2("diesel", "turbo")
self._engine = engine
def get_gearbox(self) -> None:
# Reads the car gearbox and stores it
gearbox = CarGearboxTemplatePatternV1("manual")
# How to trigger this and where to store this???
Also, how to handle the below versions in the future,
version 3
carname = b
cartype = sedan
wheel = "Alloy", number = 4, vendor = "xxx"
carengine = petrol, enginetype = turbo
gearbox = automatic, gearboxtype = cvt, gearboxicu=intel
Note: The wheel type is now changed from int to a Object
version 20
carname = c
cartype = hatchback
wheel = "Alloy", number = 4, vendor = "xxx"
carengine = battery, battery_capacity = 75
Note: The gearbox is made optional in this version
Also, is template design pattern a right choice or should I prefer some other design pattern?