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?
10
I would write loader per each version, completely separate codes to guarantee I don’t mess something. Then I would write converters (of data in memory) from version N to version N+1.
Then I would load a header of the file to detect version. Let’s say it would be K. So I would execute loader for version K, then converter to K+1, to K+2, to K+3… until to current version.
I would split the task into two parts:
-
Parsing the file. It looks reasonably simple to write code to parse this text file into a python dictionary. And, from what I can see in your examples, this code should be fairly stable. If your client ever changes to JSON or something more “standard” (er, sane), you have isolated that change to this module of code. “Easy” to change.
-
Accessing that data in a robust and reasonable manner, since the underlying dictionary structure is highly subject to frequent change across versions. This is where some type of design pattern could come into play. To be honest, I’m not sure which would be best, YMMV. (I’m thinking Chain of Responsibility.) But at least you have focused on a single task.