mardi 14 mars 2023

How to design the content of a class (or more classes) considering that each field in the class is a complex object?

I would like to create the following class system, but I came across a design problem.

abstract class StatisticCalculator
{
    FileReader Reader;
    
    StatisticCalculator(string path, ...)
    {
        Reader = new FileReader(path);
        
        // ...
    }

    // ...
}

abstract class Coder
{
    int[] Array;

    StatisticCalculator calculator;

    Coder(int size, string format, ...)
    {
        // ...
    }

    // ...
}

class Algorithm
{
    Coder ConcreteCoder;
    
    Algorithm(int coderType, int optimizationLevel, ...)
    {
        // ...
    }
      
    // ...
}

As you can see, I respected the SRP principle, by creating many classes, each of them having its own role. The main problem is that the Algorithm class contains a Coder object, and in order to create an instance of this Coder, the Algorithm constructor (or other methods) should be given the parameters required for building a Coder. Moreover, some of the fields in Coder, such as StatisticCalculator, require some specific parameters again, ans so does the FileReader in StatisticCalculator, and so on and so forth. It's a very frustrating problem, because even though the SRP principle is served, nesting objects like this would require to have all parameters available in Aglorithm constructor (in this case) and then to propagate them back. Another issue is that, for example, in the StatisticCalculator class, I might want to instance different types of FileReader, unknown beforehand (some sort of an AbstractFactory), but that would require extra parameters and I am back to square one.

To me, it's clearly a design problem. I tried to think of a Factory, AbstractFactory or another design pattern, but I haven't found any solution yet. I guess there must be a well-known recommended design solution to this problem.

Aucun commentaire:

Enregistrer un commentaire