I’m developing an java API for persistence of objects where y use annotations for the fields, but i’m not sure what would be the better implementation regarding to Classes.
public interface Persistent{
public Key getKey();
public void setKey(Key key);
}
public class PersistentObject implements Persistent{
Key key;
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key=key;
}
}
OR
public @interface Persistent {
}
@Persistent
public class PersistentObject {
Key key; //the coder must create this variable or the system doesn't work
}
- The firs one use the interface mechanism that is widely used in OOP,
it requires to create a variable to implement this interface but is
supposed the progammer knows it. - The second is easier for the final programmer and is widely used in many
libraries for persistence however forces the programmer to create a
variable with one name by convention that doesn’t fit in the OOP
model.
Thanx for your answers.
No, I am not going to give you a answer. But I suggest that you go through the chapter “Prefer annotations to naming patterns” in Effective Java by Joshua Bloch.
Please let me know if that’s the answer you are looking for.