0

I have the following:

foreach(var file in today.GetFiles())
{
    if(file.Length > 0 && file.Extension == ".txt")
    {
        switch (file.Name)
        {
            case "realy_long_ugly_file_name_0":
                //do something
                break;
            case "realy_long_ugly_file_name_1":
                //do something else
                break;
        }
    }
}

I have come across this post and thought I could clean up my code using an interface. Would this be the right application for this situation? I have set up the interface and the inheritance but don't quite know how to proceed.

I understand the interface is calling different a class based on what IPizza member is in the IList<IPizza>. I have a hard time understanding how I can pass in file.Name (formerly done with a switch/case) to my interface to call different classes.

interface IMyFiles
{
    void Process(FileInfo file);
}

public class FileName0 : IMyFiles
{
    void IMyFiles.Process(FileInfo file)
    {
        //do somthing specific to FileName0
    }
}

public class FileName1 : IMyFiles
{
    void IMyFiles.Process(FileInfo file)
    {
        //do somthing specific to FileName1
    }
}
Community
  • 1
  • 1
Kyle
  • 5,407
  • 6
  • 32
  • 47

2 Answers2

0

What you are aiming here is to encapsulate the different types of processing based on files into is own objects.

These are the options that I see:

  1. IMyFiles objects decide what do they process(you can do this by having an if at the start of the methods for the concrete implementations). Basically make a collection with all your classes that are the IMyFiles and call process until someone knows it needs to pick it up.
  2. Create a factory using your switch statement so that it returns the instance of IMyFile and then just call process. This way the decision of what i am(construction) and what can i do(are separate).
Alfredo Alvarez
  • 336
  • 3
  • 15
0

if the all file having similar columns but variation in numbers like first have 3 columns and other have x number of columns, we can have other choice of programming, but if the file are totally different then this is nice code, it readable and documented