Using Java, how can I restrict an object property to have certain values? I want to create a Java object that represents a “type of location” but I want to restrict the use of the class to only about 100 strings representing all possible types? What is the design pattern for this?
All I can think of is to create a String arraylist and each time a user instantiates the object I would iterate through the entire list looking for a match. That seems sorta like a hack to me though and I want to do it right.
How about using Java’s Enumerations? Your Object would just by the type of that Enum and then you’d be bounded by the 100 or so Strings you have in your enum.