Commandline: Why is DefaultSentenceBuilder private?

Created on 31 Aug 2019  路  2Comments  路  Source: commandlineparser/commandline

Is there a reason that DefaultSentenceBuilder is marked private? It would be much easier to customize SentenceBuilder if the class were public and could be inherited.

public class MySentenceBuilder: DefaultSentenceBuilder
{
   public override Func<string> UsageHeadingText => () => "Usage:";
}

SentenceBuilder.Factory = () => new MySentenceBuilder();

Another possibility would be to make SentenceBuilder non-abstract, move the default implementations there from DefaultSentenceBuilder, and then get rid of DefaultSentenceBuilder altogether.

question

Most helpful comment

Sorry, I wasn't clear. My question was whether there was a design reason for not making DefaultSentenceBuilder a public class in the first place.

If this class were moved into its own file and turned into its own public class, I don't think that would be a breaking change -- the class is currently not visible, so adding it as a visible class wouldn't affect anyone's current usage.

The problem with inheriting from the abstract SentenceBuilder is that I would have to provide implementations for _all_ of its properties, whereas inheriting from DefaultSentenceBuilder lets me override only the behavior I want to change, which is much simpler (as in my example above). I think this would be much friendler for users of the library.

(There's another workaround that involves grabbing a copy of DefaultSentenceBuilder before setting Factory and then delegating to that copy in order to inherit behavior, but it's still more work than just inheriting from DefaultSentenceBuilder.)

All 2 comments

Is there a reason that DefaultSentenceBuilder is marked private?

DefaultSentenceBuilder is a nested private class for the class SentenceBuilder.
Nested types default to private; they are accessible only from their containing type.
Although it can be public, it will be accessed as:

     public class MySentenceBuilder: SentenceBuilder.DefaultSentenceBuilder
     {
       //.....
    }

and cause breaking API.

It's preferred to inherit from the public abstract class SentenceBuilder

Another possibility would be to make SentenceBuilder non-abstract, ...

This can break the public API of the library. It's used by the localized demo

Sorry, I wasn't clear. My question was whether there was a design reason for not making DefaultSentenceBuilder a public class in the first place.

If this class were moved into its own file and turned into its own public class, I don't think that would be a breaking change -- the class is currently not visible, so adding it as a visible class wouldn't affect anyone's current usage.

The problem with inheriting from the abstract SentenceBuilder is that I would have to provide implementations for _all_ of its properties, whereas inheriting from DefaultSentenceBuilder lets me override only the behavior I want to change, which is much simpler (as in my example above). I think this would be much friendler for users of the library.

(There's another workaround that involves grabbing a copy of DefaultSentenceBuilder before setting Factory and then delegating to that copy in order to inherit behavior, but it's still more work than just inheriting from DefaultSentenceBuilder.)

Was this page helpful?
0 / 5 - 0 ratings