This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Placement of a read method

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I had a discussion with a colleague about the placement of a method. The setup is this: I have project based on the MVC pattern. I have a controller, I have a User-class and I have a class that manages my input and output. Now my user tries to login. I thought my controller should ask the database for a User object and use this. My colleague said that it would be better to create a new User object in the controller, and leave reading to it (so that the user knows about the input and output class). Same goes for saving the User (in case of changes).
Both versions work, of course - but what would be better?
 
Winston Gutkowski
Bartender
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:I had a discussion with a colleague about the placement of a method. The setup is this: I have project based on the MVC pattern. I have a controller, I have a User-class and I have a class that manages my input and output. Now my user tries to login. I thought my controller should ask the database for a User object and use this. My colleague said that it would be better to create a new User object in the controller, and leave reading to it (so that the user knows about the input and output class). Same goes for saving the User (in case of changes).
Both versions work, of course - but what would be better?

Interesting question.

On balance, I would say I agree with your colleague: that the login process should be part of the controller, and that it should hand you back a fully-functioning User object - and certainly one that knows how to save itself.

However, that doesn't preclude you from having an object that handles I/O; it just means that functions that have anything to do with a User are dealt with by that User - I presume there's nothing to stop your User object from calling your I/O manager if it needs to.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic