3

I'm writing an API, in which one of the function calls of which is meant to be a call to a factory for a series of tree nodes that take a number of parameters, some of which are the same and some of which are different. The kicker is that it's on the user to specify the exact structure of the tree - doing it algorithmically would go against what I'm trying to provide. The question I have is, what is the best (most intuitive, mostly) way to structure the function call? So far, my best idea is something like this:

void FooClass::createTree(
    {
        {2},
        {4, 5},
        {2, 2, 2, 2, 2, 2, 2, 2, 0}
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    },
    {
        {"N1"},
        {"N2-1", "N2-2"},
        {"N3-1", "N3-2", "N3-3", "N3-4", "N3-5", "N3-6", "N3-7", "N3-8", "N3-9"},
        {"N4-1", "N4-2", "N4-3", "N4-4", "N4-5", "N4-6", "N4-7", "N4-8", "N4-9", "N4-10", "N4-11", "N4-12", "N4-13", "N4-14", "N4-15", "N4-16"}
    },
    other_args...
)

Where, obviously, a 0 would mean the termination of a particular branch. This is being built in C++, and is meant to be provided as part of a library function where the tree is used internally by FooClass.

I realize that, ultimately, I'll need different calls for different combinations of varying/non-varying parameters, but what I'm stumped on is the "base case" format. I'd also like to avoid using a Builder object (as has been suggested), as the tree that is ultimately specified by the user must be constructed from the bottom up (a result of the problem domain - this is an unavoidable constraint).

  • 1
    I assume this is a fat-client API, not HTTP? What language are you using? Is there a a reason you're not using a Builder? – Eric Stein Jul 8 '16 at 18:05
  • Yes, a fat-client API, in C++. I don't really see how a Builder of some sort would really solve my problem, though - the problem is the recursive structure of a tree, which doesn't map well to function calls. – Sebastian Lenartowicz Jul 8 '16 at 18:37
  • 4
    VTC as Unclear What You're Asking because I look at your sample code and I don't see anything that intuitively looks like a tree structure, or any explanation in the question of why this is supposed to represent a tree. – Mason Wheeler Jul 8 '16 at 21:08
  • @SebastianLenartowicz Maybe require that the user's classes used in building the tree conform to an interface that include your necessary accessor functions. – Ouroborus Jul 8 '16 at 21:47
2

I decided to go with a simple builder object after all. The idiomatic call looks a bit like this:

FooClass.createTree(
    TreeBuilder("N1")
        .addSubNodes("N1", {"N2-1", "N2-2"})
        .addSubNodes("N2-1", {"N3-1", "N3-2", "N3-3"})
        .addSubNodes("N2-2", {"N3-4", "N3-5"}),
    other_args...
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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