Assume I have these classes:
@Entity
@Data
class Student{
@Size(min = 8)
public String name;
public int age;
...
}
@Data
class StudentDTO{
@Size(min = 8)
public String name;
....
}
To reduce duplicate annotations, I've figured out a simple trick like this:
interface HasName {
@Size(min=8)
String getName();
}
@Entity
@Data
class Student implements HasName {
public String name;
public int age;
}
@Data
class StudentDTO implements HasName{
public String name;
}
There could be more fields.
Pro:
- No duplicate annotation
- Annotation will be unified
Cons:
- Increase complexity
- Increase dependency
But it look like a code smell to me, so is it good or bad?