0

I have a class which call country specific functions for different use cases. I want to generalise the logic in such a way that if I specify the country name & the method name, the same method for that country's class gets executed.

Predicate Class:- https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

private static void countrySpecificMethod(String countryName, Predicate predicate){
        if(countryName.equals("EGYPT")){
            // call the predicate function in EGYPT class
        } else if (countryName.equals("INDIA")) {
            // call the predicate function in INDIA class
        } else if (countryName.equals("HUNGARY")) {
            // call the predicate function in HUNGARY class
        } else if(countryName.equals("TURKEY")){
           // call the predicate function in TURKEY class
        } else if(countryName.equals("ITALY")){
            // call the predicate function in ITALY class
        } else if(countryName.equals("VIETNAM")){
            // call the predicate function in VIETNAM class
        } else{
           // call the predicate function in same class
        }
    }

Example:-

countrySpecificMethod("EGYPT", prepareAddress) 
 It should then call EGYPT.prepareAddress()

countrySpecificMethod("EGYPT", prepareUser)
 It should then call EGYPT.prepareUser()

Any help how this can be achieved?

Diksha Goyal
  • 260
  • 6
  • 19
  • What is a predicate function in this context? – aled May 15 '23 at 11:15
  • https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html – Diksha Goyal May 15 '23 at 11:28
  • That is not really useful. What are you trying to actually achieve? – aled May 15 '23 at 11:34
  • Ohh got it, so I want to achieve a High order kind of Function here, where predicate for example can be `prepareAddress` & country be EGYPT. So the countrySpecificMethod should then call EGYPT.prepareAddress(); – Diksha Goyal May 15 '23 at 11:39
  • I just wanted to show what I need to do. It obviously will be a function name – Diksha Goyal May 16 '23 at 10:55
  • 1
    But a `Predicate` is not a function name. It’s not clear why you think this interface is suitable to your task at all. Which is one of the reasons why no-one understood your actual task so far. – Holger May 22 '23 at 12:07

1 Answers1

0

You could have a lookup table to find the object from the appropiate class for the country, and lookup for the string identifier that matches the country.

For example a Map implementation, using a String as the key, and an interface or abstract class that defines the methods that can be called. It would look something like this:

Map<String, CountrySpecificInterface> countryLookup = new HashMap<tring, CountrySpecificInterface>();

countryLookup.put("EGYPT", new EgyptImplementation()); // EgyptImplementation implements CountrySpecificInterface including method prepareAddress()

countryLookup.get(countryName).prepareAddress()

You should put the string literals somewhere, to avoid repeating them all over the code. For example use an Enum.

aled
  • 21,330
  • 3
  • 27
  • 34