vendredi 10 janvier 2020

Which design pattern should I use in this case?

I have a class named DS which can (1) read a data from file and accordingly build a data structure from scratch, or (2) read a pre-built data structure from file. I originally wrote:

class DS 
{
    DS(std::string file_name, bool type);
}

where file_name is the file to read and type specifies what we are reading, data or pre-built data structure. This method is not very elegant, as far as I am concerned. I also tried the following:

class DS 
{
    DS(std::string file_name);
    void CreateFromData();
    void ReadExisting();
}

But because modification is not allowed once built, I do not want the user to first call CreateFromData and then ReadExisting.

Are there some design patterns to address this issue?

Aucun commentaire:

Enregistrer un commentaire