It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Placement of a read method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Placement of a read method" Watch "Placement of a read method" New topic
Author

Placement of a read method

Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

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?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7536
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Placement of a read method
 
Similar Threads
OCP book (K & B) - Exam2 - Num 50 - GC question
Adapter or Mediator?
Doubt In MVC..
ActionPerformed happens twice...
MVC questions