I have two classes, namely
User & UserProfile
User contains(few attributes like):-
- userId
- loginId
- password
and
UserProfile contains(rest of all attributes like):-
- userId
- name
- address
- mobile
- dob
I have two tables in the database corresponding to these classes namely User_table & User_profile_table
Now what I want is to design these classes in such a way that it accrues a perfect OOPs concept.
My question is beside the setter & getter methods what are the other methods do I need in these classes.
I planed to put a
- constructor UserProfile(Integer userId) in UserProfile class which will take userId, get the values from User_profile_table and initialize all attributes.
- another constructor UserProfile() with no parameters to initialize an object with null values.
- a saveUserProfile() to save the profile in database
I just need to work with the database like:-
For User class methods like
– saveUser()
– retriveUser()
And Similarly with the UserProfile
Shall I put the codes dealing with the database inside the saveUser and retriveUser methods?
If no then how can I handle those codes(where should I write the code dealing with the database)?
Some classes are value types, representing a piece of data. For these, getters and setters might be all you need.
Using getters/setters rather than public fields leaves open the possibility that you may change the representation later, without affecting callers.
Each class should have a single responsibility. If it represents a user profile, that’s all it should do. You can place additional functionality in other classes — e.g., a UserValidator.
At some point, you may become interested in object-relational mapping (ORM) tools such as hibernate and ibatis.