Liam de Haas
Liam de Haas

Reputation: 1268

Where to put an ArrayList in the mvc model

I have three classes, User, UserController and UserView, that look a bit like this:

public class User {
    private String userName;
    private String password;

    //Constructor

    //Getters and Setters
}

public class UserController {
    private UserView view;

    //methods
}

public class UserView {
    //methods
}

I need to have an ArrayList<User> allUsers because I have multiple users, but where do I put it?

My initial thought was the controller class but what i've learned about the MVC pattern is that theview 'executes' the controller which then updates the model. the model notifies the view it has been updated and then the view pulls the data from the model. So if I put it in the controller the view has to pull data from the controller, which isn't following the MVC pattern.

Do I need to make a class Users that holds the ArrayList<User> so the view can pull the data from there?

Upvotes: 0

Views: 3177

Answers (3)

RKP
RKP

Reputation: 99

This is the case where designer need to take care of this issue and there must be clear line between controller, model and view. Controller must control the flow of application and view must take care of the view part and show the model to frond end. Now you want to return the multiple model user, that must be take care at the business layer where you will be updating model(date), so for this the must be another class where you will process your data and return back to controller.

Upvotes: 0

null
null

Reputation: 5255

I feel like the question answers itself.

You already grouped multiple objects of the same type into a model: the user name and password. Your UserView probably displays both of them.

You didn't ask yourself if it's valid to have many strings in one model, you just defined the model in a way that fits your needs. Maybe there is a StringView to go along with them maybe not.

You are in the same situation again. If grouping things into models is what you need, go ahead and do it. If you need additional Views depends on what you want to achieve. Maybe you can create the UsersView by instantiating many UserViews and dispaly them in a grid or maybe you'd only instantiate one and swap out the model from the list of models with arrow buttons. This all depends on what you want to do.

Upvotes: 0

committedandroider
committedandroider

Reputation: 9291

Yeah, the idea of the Model View Controller is to have the controller(listener) update the view(GUI) when the model(the data) changes or update the model when the view is interacted on. So have something like

public class Users{
     private List<User> users;
   ....
 }

And then in your controller, take in a Users instance and a View so it can update(or act on) both accordingly.

   public class UserController {
          private Users toUpdateUsers;
          private UserView toUpdateView;
          public UserController(Users theUsers, UserView view) {
                toUpdateusers = theUsers;
                toUpdateView = view;
          }
          ....
      }

Upvotes: 0

Related Questions