mardi 26 février 2019

Does single responsibility principal ideally means striving for a class that has a single member function?

I know about single responsibility principal that class should have responsibility for one thing, or should have only one reason to change or should just do one thing.

The question is about the art of achieving this goal.

Should we be essentially striving for a class that have essentially just have ONE main method and of course it may have other helper methods?

In other words, we are kind of replacing a traditional long function with a class?

For example

class ExportFile
{
public:
    virtual void Export(string fileName) = 0;
};

class ExportExcel : public ExportFile
{
public:

    virtual void Export(string fileName) //= 0;
    {
        cout << "Export data to excel" << endl;
    }
};

int main()
{
    string fileName = "c:\\test\\myfile.dat";

    ExportFile * excelExport = new ExportExcel();

    excelExport->Export(fileName);

    return 0;
}

I do see advantages this way that I can implement different strategies easily if I want to export file to say a pdf because I only have to override one simple function and create other internal functions as needed.

I guess I am looking for validation. Could this be frown upon that this class is too light, does too little and it should be better left as method of some other class?

Aucun commentaire:

Enregistrer un commentaire