4

For string constants its usual to use a class with final String values. But whats the best practice for storing string array. I want to store different categories in a constant array and everytime a category has been selected, I want to know which category it belongs to and process based on that.

Addition : To make it more clear, I have a categories A,B,C,D,E which is a constant array. Whenever a user clicks one of the items(button will have those texts) I should know which item was clicked and do processing on that.

I can define an enum(say cat) and everytime do

if clickedItem == cat.A
 ....
else if clickedItem = cat.B
 ....
else if 
 ....

or even register listeners for each item seperately.

But I wanted to know the best practice for doing handling these kind of problems.

3 Answers 3

6

I much prefer using Enums to store system wide string constants, makes it easy to create additional functionality and I think it is generally accepted best practice.

public enum Cat {
     A() {
          @Override public void execute() { 
               System.out.println("A clicked"); 
          }
     },
     B() {
          @Override public void execute() { 
               System.out.println("B Clicked"); 
          }
     };
     //template method
     public abstract void execute();
}

then you can call it like so :

String clickedItem = "A";    
Cat.valueOf(clickedItem).execute(); //will print A is clicked

or you could also the Command pattern without enums....

public interface Cat {
     void do();
}

public class CatA() implements Cat {
     void do() {//put your code here}
}
public class CatB() implements Cat {
     void do() {//put your other code here}
}

then build a Map object and populate it with Cat instances:

catMap.put("A", new CatA());
catMap.put("B", new CatB());

then you can replace your if/else if chain with:

catMap.get(clickedItem).do();
4

Your description is quite vague, so maybe I got this wrong, but it sounds as you could use a Map<String,String> storing your category relationships. E.g. if you have

class Foo {
   final static String[] cat1 = {"val1_1","val1_2","val1_3"};
   final static String[] cat2 = {"val2_1","val2_2"};
}

I would write:

class Foo {
  final static Map<String, String> map = new HashMap<String,String>();
  {
     map.put("val1_1","cat1");
     map.put("val1_2","cat1");
     map.put("val1_3","cat1");
     map.put("val2_1","cat2");
     map.put("val2_2","cat2");
  } 
}

Then getting the category is as easy as calling Foo.map.get("val1_1");. Of course you should be a little bit more clever than me filling when filling the map (e.g. from a file or so). If you are concerned with changes of the map, wrap it using Collections.unmodifiableMap.

1
  • 2
    To guarantee that the values aren't modified, you could wrap the map with Collections.unmodifiableMap(). Nov 15, 2011 at 16:55
-1

As for your question - it is unclear what exactly you want to achieve, but I could just give you some general advices about using enum:

  • An enum constant is an object and enum class name should start with uppercase (Cat)
  • An enum is a fixed, finite set of enum constants. Don't use where you need variability.
  • Enums can reference other objects. Final references of immutable objects are recommended. This can be very useful e.g. to store your string constants.
  • Enum constant can be used as a map key, but also as a switch-case constant. It is definitely nicer than using a long if-else tree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.