1

Let’s say I have four classes A,B,C,D.

Each of those classes contains a Tree object. This tree object needs to be built according to information stored in files f_A, f_B, f_C, f_D, for classes A,B,C,D respectively.

So the tree in class A needs to be built according to f_A.

Now I think it is not a good idea for the Tree class doing file I/O.

One option would be that the Tree has a function buildFromString(String s) which takes the content of a file as a string and builds itself.

But again, I don’t think the Tree should do any String processing.

An alternative would be that for each class A,B,C,D we have a Tree builder Class, which takes a Tree object as a parameter, reads the corresponding file and builds the Tree.

Maybe a Factory pattern would help, but I don't see how to make use of it in this context.

But this introduces a lot of new classes.

What would be a clean way of building the trees from the files subject to which class they are instantiated from?

3
  • But this introduces a lot of new classes. ...so what?
    – null
    Commented Jun 18, 2015 at 16:38
  • What are the differences between those classes except for building their respective tree from a different file? The whole "build tree according to file" could go into a super class.
    – null
    Commented Jun 18, 2015 at 16:49
  • @null Yes the whole tree building functionality would go into a superclass, the respective classes would basically just contain the correct filename for the tree to be built from
    – user695652
    Commented Jun 18, 2015 at 17:39

2 Answers 2

1

The question how "clean" it is to have a generic builder/deserializer/factory that can create objects of different classes from textual descriptions depends on how easy this is to do in the language. In JavaScript it is trivial, since the JavaScript Object Notation is built into the language. In Java it is possible but a bit harder, since you have to muck around with reflection and explicit classloading. In something like Ada (I think, correct me if I'm wrong) it is hard to impossible.

Therefore, whether it is better to go for the meta-approach or create a parallel hierarchy of builder classes depends on how efficient, readable, maintainable etc. these alternatives are on the platform you will be using. It is almost certainly not ideal to have to create two classes for each concept in your model unless you can automate the process, but depending on circumstances, it might be the lesser of two evils.

1

the whole tree building functionality would go into a superclass, the respective classes would basically just contain the correct filename for the tree to be built from

I don't think creating subclasses just to hardcode some values is a good idea. A,B,C and D are basically speaking just values of the same type.

In a sense, you are looking a for a way to define specific values of the single type that is the super class (with all the file and tree handling). Like an object literal.

Depending on the language you are using, you could do this with static methods, that basically call the constructor with a hardcoded value

some pseudo code

TreeThing (String filepath) // constructor
{
    // do the tree stuff, dependign on the file
}

static TreeThing A ()
{
    return new TreeThing("fileA");
}

Your Answer

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

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