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 obje开发者_Python百科ct 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.
I'd make you change your table names: "user" and "user_profile" should be sufficient unless you have a naming convention that forces you to name them that way.
What might be useful?
- Two methods to calculate age based on current or given date for the DOB of the user.
- Validation to ensure that the credentials for the user are unique and follow your standards.
Your application will tell you others. You should see it as an opportunity to encapsulate behavior inside an object when you find yourself calling a getter and manipulating what you get back.
There is no "perfect" answer. The model always depends upon the overall context (which you know more than anyone). But with the little information you have provided below are some methods I can see that are part of User
- authenticate()
- isAuthenticated()
Repository interface
- addUser(User u)
- findUserById(...)
If you ask me, the only functionality I would put in a Data Transfer Object is validation and methods that let you retrieve some type of object representation (hashcode, toString, toJson, if it is simple). Things such as authentication, database access and so on, are separate concerns and they belong to other components, accordingly.
The most important part you can also include toString(), equals(), hashCode()
as per your convenience... This are the most important methods in production level.
As far as OOPs is considered and my knowledge purview is considered. I would like to give you some advice:
1) Remember your domain must tell and not ask.
As I can see in your problem, in your table hierarchy User_table is of high importance than User_profile_table. So you should tell User_profile_table about user and not ask from it the object of User.
2) Take care of if there is bidirectional association ,but I don't see there is, you don't couple the state of two objects too tightly.
Refer http://priyaaank.tumblr.com/post/95095211355/objects-that-are-loose-discrete .
精彩评论