3

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?

1 Answer 1

3

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.

4
  • I will change the name accordingly. So you are saying instructionSet contains instructionMapping class and the instructionMapping is there just for the substitution (via map) ?
    – vanta mula
    Oct 28, 2017 at 18:38
  • 1
    Yes, that's what I'm suggesting. Oct 28, 2017 at 19:05
  • one last question though. So class instructionSet would contain something like std::map<std::string, std::vector<std::string>. Then in line where you said, 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. you mean building return value? for eg: when user request a specific step, by doing something like obj.get_step("step2"); eg: step 2 we return std::vector<int> eg ` {101, 233, 901}`
    – vanta mula
    Oct 28, 2017 at 19:11
  • Exactly! It seems like an easy way to do it unless you need the variable names in order for some other purpose. Oct 28, 2017 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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