vendredi 8 novembre 2019

Design pattern advice - Reading data from TXT

PROBLEM

I have an application which requires loading most of its data from .txt files (which are previously given and exist on local storage - they don't need to be fetched). Let's say I currently have 3 models: Person, Role, Task with their associated data Persons.txt, Roles.txt, Tasks.txt respectively. Each file has a header which describes the structure of each entry (e.g. id; name; surname), and each line in .txt file represents one entry (excluding header). Having said that, entries can be in wrong format (different from header) and those must be skipped.

GOAL

I want to somehow create a generic data loader which will load the data (read .txt line by line, validate entry, trim and convert to object instance) and return list of objects depending on the class I pass to them. Example, DataLoader<Person> for loading only person data. The current solution uses Singleton pattern for each class I named, and also containts logic for parsing, validating and converting data. The reason for that is mostly because the data must be accessible from all other parts of application, but each class contains too much logic and most of other classes rely only on these Singletons - which is not good in my opinion.

QUESTION

What is the best (cleanest) design pattern to implement in this use case? Few things to notice before answering:

  1. The file format will never change (it will always be .txt)
  2. There might be more data to load in future (like Department and Departments.txt and so)

Aucun commentaire:

Enregistrer un commentaire