mardi 16 mars 2021

Best practices for creating a lot of constants that are externally defined

I have been doing some pattern that I am not exactly sure that name is. My Java heydey was release 1.4 and programming has not been my day job for many years.

My pattern is this:

/**
To use one of these programmatically, use Foo.getInstance( String ); or to compare them, 
use ( Foo.A == Foo.B )
*/
public final class Foo extends HasChildren {

    public static final Foo FOO_BAR = new Foo( "Foo bar" );
    public static final Foo BAZZY_BAZ = new Foo( "Baz bar" );
    // and so on, with hundreds of these constants

    FOO_BAR.addChild( BAM );
    // and so on, with hundreds of these initializer routines

    public static final getInstance( String name ){
        if ( name.equals( "Foo bar" ) ) return FOO_BAR;
        else if ( name.equals( "Baz bar" ) ) return BAZZY_BAR;
        else if ( yadda yadda )
        else return new Foo( name ); // breaks "the pattern" but at least program will run!
        // else throw new ExceptionInInitializerError( "unmatched" ); // alternate ending
    }

    private final String name;
    private Foo( String name ){
        this.name = name;
    }
}

I get the structure of these things in a JSON file and to load these, (once I found out how many there are) I just parse the JSON and

System.out.println( "public static final Foo " + name.toUpper() etc. etc. + ");" );

and then I paste the result into my code. You can see that I have some limited ability to deal with outliers at runtime with my catch-all in the factory method.

My question is "is there a pattern to do this elegantly?" I don't consider use of java.lang.reflect to be elegant, but that is definitely a way to do it. What I am thinking is that the many years of advancement of the language may have provided something better than what I am doing. And because I am tongue-tied on putting a name to the patter, I haven't had an easy search for a better way.

Aucun commentaire:

Enregistrer un commentaire