Java design pattern for storing a method name in a properties file

I have a couple of property files (key / value) where I am currently reading the value against the key and display that value as it is in the user interface.

The difficulty has increased. Now the value is more dynamically based on some formula. The formula includes a variable parameter, the value of which I will get at runtime.

Is there any java design template for developing this script.

I was thinking about putting the method name in the property file with the key.

Now I will read the key and select the name of the method. This method will calculate the value for this particular key.

Please let me know your suggestion.

+5
source share
6 answers

Is there any java design template for developing this script.

I do not know if there is a template.


If I understand your question correctly, I can explain what I usually do .

  • Insert localized strings into the values โ€‹โ€‹of my properties
    I usually use #number#
  • Replace it later when resolving variables

A small example:

messages.properties

 name.of.key = sum of #0# + #1# = #2# 

Then I read the value and replaced #num# with the appropriate values โ€‹โ€‹( NOTE: this uses the same method for abbreviations, but I use the external replace method):

 public void printSum(int n1, int n2) { String myString = messageSource("name.of.key", Locale.getDefault(), null, null)); myString.replace("#0#", String.valueOf(n1)); myString.replace("#1#", String.valueOf(n2)); myString.replace("#2#", String.valueOf(n1+n2)); System.out.println(myString); } 

EXIT printSum(1,2);

 sum of 1 + 2 = 3 
+1
source

Use the Java built-in JavaScript engine to evaluate expressions. To more closely match the spirit, you can use JSON for properties.

If security is important, you need to provide a class filter . This can be very simple and restrictive as you only need to evaluate trivial expressions. An example of a class filter can be found here .

+1
source

It seems that ANTLR will attach great importance to this.

This is a parser generator. You give it a grammar as input and in turn provides you with a parser.

You can use the parser to convert a text formula to a syntax tree view. After that, you can run the visitor to evaluate each of the nodes. You just write a simple function to implement the behavior, for example:

 public Double visitAdd(AntlrNode left, AntlrNode right) { Double left = visit(left); Double right = viist(right); return left + right; } 

Grammar is very close to the familiar BNF notation. You just describe how your formula lines. For instance:

 formula : left '+' right; left: Number; right: Number; Number: [0-9]+; 
+1
source

You can use the strategy template by putting the name of the method / algorithm in the property file:

 public interface IFormula{ public int formula(int a, int b); } public class Sum implements IFormula{ public int formula(int a, int b){ return a+b; } } 

Then you can choose how to get the name from the properties file:

 public static Strategy getStrategy(Name name) { switch (name) { case SUM: return new Sum(); ... } } 
0
source

Another solution is to reorganize your map so that the value type is a functional interface, the method of which takes an arbitrary parameter. For instance:

 @FunctionalInterface interface ValueType<R> { R eval(Object param); } 

This solution (or its variant) will allow you to associate a lambda with your keys, rather than a fixed value. Lambda performance should be much better than a runtime analyzer, still giving you the flexibility to make the bound value depend on the argument at runtime.

This solution should also be less vulnerable to injection attacks than a solution based on runtime analysis.

0
source

Since it seems to you that a name is required for the template ... the template is called: Domain-specific language .

And again, if you want to stay in the realms of abstract models and design, you can see Martin Fowlers' discussion on the topic .

Needless to say, they represent a metric ton of tools that solve the above pattern (including some of the answers here).

Another template that I highly recommend to you, NOT , is to use a general purpose language with an evaluator (e.g. Javascript, EL, Groovy, etc.). These are usually security and performance issues (of course, there are exceptions).

0
source

Source: https://habr.com/ru/post/1236402/


All Articles