2

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
  • 3
    Have you considered using JSON or XML or YAML etc?
    – user949300
    Sep 20 at 5:07
  • 4
    Before thinking about a solution, start with thinking about the requirements. You forgot to tell us if the file format is a requirement, given by someone else to you, or if you have the file format and its evolution under your own control (but get some external requirements to change it accordingly). Please clarify.
    – Doc Brown
    Sep 20 at 5:09
  • Thanks @DocBrown. I cannot change the file and If it was possible, I would have already changed it to a structured format like json or yaml. But, even then, the problem persist, right?
    – Selva
    Sep 20 at 6:01
  • Further, The format and the content evolution is not under my control but I get informed when a new file version is available to adjust my code
    – Selva
    Sep 20 at 6:23
  • 1
    Lets stick to the requirements. I assume your code is ready to work with car objects of the latest version (lets say V20), and all you need is a way to read car objects of older versions from text files into a "V20-car object"? Or is there any requirement for dealing with older and newer car versions in parallel (beyond deserialization)?
    – Doc Brown
    Sep 20 at 6:30

1 Answer 1

0

I would split the task into two parts:

  1. 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.

  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.