java - Design patterns for type-safe integers? -


I have one such app with an integer ___ID primary key (like student ID, course ID ... etc)

I want to add some type of security to the program, for example, a function, taking a student ID as input, can not be accidentally pass Crores.

The first idea is to make such a simple empty square in my mind:

  public class studentID java.util.Integer {} public class CourseID java.util.Integer {} < / Code> / Pre> 

This does not work since java.util.Integer last and can not be expanded.

The second idea is to keep a data class which only holds one integer:

  public class StudentID {Private Final IND; Get public int () {return ID; However} this will be a lot of boilerplate code.  

The third idea is like a second thought but to protect the boilerplate code from a base class which defines method and empty classes obtained from it:

  AbstractID of the public abstract category {Private Final ID; Get public int () {return ID; }} Public Square StudentID AbstractID Extended {}; Public Classroom CourseID AbstractID Extended {};  

Are there any other / better known patterns for this problem?

How about the ID & lt; T & gt; ? If you want to restrict some logic to the function, then I think that you can do the following:

  void foo (id & lt;? Extended student & gt; param );  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -