1

Currently I am creating a java Project that will be used by many people to (in theory) create a product, each with their own set of configurations, in a straightforward, fast and with standardized mechanisms.

If I were using a GUI, it would be easier to handle the configurations in different files, however, because I am making it just an editable project, I see myself in the need of putting all the constants in a single class/file so as to centralize the area where the respective engineer configures the product he is creating. Thus I have the following Constants interface:

public interface Constants {

    //ROUTE OF FOLDERS
    String PATH_ACTUALIZ = "..\\actualiz\\";
    String PATH_ENTRADAS = "..\\entries\\";
    String PATH_SALIDAS = "..\\outputs\\";
    String PATH_ENTRADAS_CARTAS = PATH_ENTRADAS + "CARTAS\\";
    String PATH_ENTRADAS_ANEXOS = PATH_ENTRADAS + "ANEXOS\\";
    String PATH_SALIDAS_PDF = PATH_SALIDAS + "PDF\\";

    //ROUTE OF FILES
    String PATH_FILE_SPOOL_CLIENTE = PATH_ENTRADAS + "spool.txt";
    String PATH_FILE_SPOOL_ERRORES = PATH_SALIDAS + "Reporte_de_Errores.txt";
    String PATH_FILE_BASE_EMAIL = PATH_SALIDAS + "BaseEmail_[OP].txt";
    String PATH_FILE_DATA_VAR = PATH_SALIDAS + "DataVar_[OP].txt";
    String PATH_FILE_BASE = PATH_SALIDAS + "Base.obj";
    String NAME_FILE_LOG = "Log.txt";

    //DESIGNER
    String DESIGNER_DELIMITER = "\t"; //Other options "»", "|"
    String DESIGNER_CANAL = "AA01";

    //GUI INFORMATION
    String NAME_RESPONSABLE = "JOHN DOE";
    String TITULO_MENSAJE_ERROR = "Error when processing the data.";
    String TITULO_MENSAJE_INFORMACION = "Processing data";

    //HTML
    String HTML = "123";

    //SEGMENTS
    HashMap<String, String> HMSEGMENTO = new HashMap<String, String>(); //Initialization missing.

}

The last line of which is a HashMap because the data involves several pairs of String to String, including one that is returned by default if the key provided isn't found, however I find myself at a standstill because there is no elegant/simple way add data to a Hashmap inside an Interface.

Other solutions I've looked into include the use of Java Properties, but I also wouldn't be able to add data to it in this interface.

So my larger question becomes if I'm even doing this right at all. Any sources or ideas on software development that would direct to the right path would be greatly appreciated.

Finally I understand my question is slightly broad. If there is a way to make it more specific, I would appreciate the help too.

Ren
  • 4,594
  • 9
  • 33
  • 61
  • Arguably, an [interface should not contain constants](https://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants). Use enums, or if the values of a key can change then create a class which wraps a Properties. – Andrew S Feb 12 '18 at 15:54
  • 1
    @GhostCat I was just waiting if anyone had anything else to say on the topic. But yeah, your answer did help a lot. I will accept it. – Ren Feb 13 '18 at 17:54
  • 1
    Sure. And I appreciate the quick comeback. And yeha, just one more upvote and 69K. About time ... – GhostCat Feb 13 '18 at 18:43

1 Answers1

1

Serious non-answer: don't do this.

Java invented Properties to exactly give you that: the ability to specify runtime configuration information in text files. And when you need multiple layers - that is also possible using property files (you could for example have a base property file that defines useful defaults, and then different people can provide their own property files that "override" those properties that they need to be changed).

And when you insist on using interfaces to "wrap" your constants (although exactly that is considered a bad practice) - please don't stuff everything into the same interface. Instead identify the different "categories" you need and have one file per category.

Your approach is determined to end up with monolithic spaghetti code.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I was not aware that Java Properties could also be used for multilayered data. I will look into it. Thank you. – Ren Feb 12 '18 at 15:55
  • @Yokhen To be precise: if you want to *layer* properties, *you* have to do some design/implementation. I don't think that the "base" implementation can do that. On the other hand, it shouldnt be too difficult to pull something together yourself here. – GhostCat Feb 12 '18 at 17:53