How to parse two file where one file contains the values of variable in another?
https://softwareengineering.stackexchange.com/questions/359868
-
22-01-2021 - |
Domanda
I have two files :
file1 : contains the value of the variable.
file2 : contains collection of steps (subset of collection of file1 ) to follow. Step contains the set of instructions.
- > file1.txt
x1 = 34
x2 = 33
x3 = 90
...
x12 = 101
x14 = 233
..
x41 = 901
...
-> file2.txt
step 1
- x1
- x10
- x11
step 2
- x12
- x14
- x41
..
step n
..
design 1:
I create class called storeFile1
and storeFile2
basically to store what ever is in the file1 and file2.
I also have created respective parser for each of them parseFile1 and parseFile2 which
populates storeFile1
and storeFil2
. Then I place storeFile1
inside storeFile2
.
Then when user ask for step1 and its instruction I return a pair of:
storeFile2 obj;
... // filled by parsing class first
obj.get_step("step1");
1) name of step (eg step 2)
2) set of instruction (eg: {101, 233, 901})
I was wondering if doing this is bad because I break single responsibility principle.
design 2 :
What I thought would not break single responsibility principle was to create another class caled class converter
that handles the conversion.
class storefile2
returns
1) name of step (eg step 2)
2) set of instruction (eg: {x12,x14, x41})
class converter
holds class storeFile1
and class storeFile2
, does the conversion
and return
1) name of step (eg step 2)
2) set of instruction (eg: {101, 233, 901}) // after conversion
Which one of the two design do you think is good? (I also think design 2 is a little cumbersome) or is there any design better then this?
Soluzione
For starters, I would not name your classes storeFile1
, storeFile2
, processFile1
, and processFile2
. I would give them meaningful names, such as instructionMapping
for file 1, and instructionOrder
or instructionSet
for file 2. I probably use standard existing classes for them (assuming I'm understanding your description correctly).
It looks like file 1 contains a map
(aka hash
, aka dictionary
) that has variable names and their associated values.
It looks like file 2 contains a list of named steps that contain variable names to execute (? or something) in a particular order. So it would also be a map
, but instead of just a map
of variable names to values, it would be a map of step names to an array
(or vector
or list
) of variable names that you could look up in the first map
to get their values.
When building the list of steps contained in file 2, you could simply substitute the variable names for their values as you build it by referencing file 1.