In a generic OO language (say C++, C#, java) I have a map of objects with a string ID and the same ID is also key of the relative object
class Foo(){
Foo(string id){this.id = id;}
string id;
string otherProperty;
}
map<string,Foo> foos;
now if i want to add an object i'd
map.add("myFirstID", new Foo("myFirstID"));
map.add("mySecondID", new Foo("mySecondID"));
I don't like to repeat strings, it will just get worst ( think to when retrieving by key it just explode ).
I can't use an enum (since in some languages like c++ it can't be easily converted to string, and i need it)
only option seems to be a cascade of
const string MY_FIRST_ID = "myFirstID";
const string MY_SECOND_ID = "mySecondID";
to then
foos.add(MY_FIRST_ID, new Foo(MY_FIRST_ID));
This solutions is better, still it's very verbose, there's still some coupling in same class and there's no enforcing on the values ( like there's for an enum );
Can you think any better pattern?
PS: another solution is to only use enum as keys and put the string as a simple field. But this require to add high coupling between Foo and FooManager ( class instantiating foo instances )