I want some String values to be available/accessible to me throughput the application in Java. Different classes will need them. I want to know the best possible way to keep and access those values throughout the application.
One way I know is to use Enum pattern in Java. Where I can associate a String value with each enum and then access it. Like given here. Best way to create enum of strings?
Second is to maintain a class of constants with String values.
What is the possible best way so that good design will be followed and everything will be accessed cleanly.
I would like to know.
public class StringValues
{
public static final String ONE = "one";
public static final String TWO = "two";
}
I am adding a little more detail.
I am going to create DB queries with these short name Strings. So while instantiating database I will use all the Strings in one place and will create queries.
But after query creation I will need a fragment/part of that String pool for a specific class so that I can register listener for the selected class not for all the Strings in the pool. Every class should know that It needs only 1-2 Strings names to register runtime listener not all the String names.
I need all Strings at one time(during start of application) then I will just need 2 or 3 or more of them but not all.
Here is the code to make you understand my exact design problem.
/**
*This class will be used to create Views in Database.
*/
class Views
{
public static final String BY_NAME = "byName";
public static final String BY_DATE = "byDate";
public static final String BY_GENDER = "byGender";
//For every String I am going to create Views in Couchbase.
}
/**
*This class knows to which Views it needs to listen to. If any change in its views occurs then
* it will take action. In case of byDate change it is intended to take an action.
*/
public class NewestMember
{
String[] viewsToQueryFor = {"byDate"};
//This class will call only these views and will register for them.
}
public class Male
{
String[] viewsToQueryFor = {"byName", "byGender"};
//This class will call only these views and will register for them.
}
public class Female
{
String[] viewsToQueryFor = {"byName", "byGender"};
//This class will call only these views and will register for them.
}
I do not want to do this. For this I have extra overhead of keeping String values in other classes.