0
\$\begingroup\$

I would like to ask about possible improvement of this code. I have created very simple code using MVC pattern. Is there any logic or anything to change? I tried to keep MVC idea.

This is model, class Customer:

package customereditor;    

public class Customer {

    private int ID;
    private String name;
    private String surname;

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

}

This is view, class CustomerView:

package customereditor;

public class CustomerView {

    public void displayCustomer(int customerID, String customerName, String customerSurname) {

        System.out.println("ID: " + customerID);
        System.out.println("Name: " + customerName);
        System.out.println("Surname: " + customerSurname);
    }

}

This is controller, class CustomerController:

package customereditor;    

public class CustomerController {

    Customer model;
    CustomerView view;

    public CustomerController(Customer model, CustomerView view) {
        this.model = model;
        this.view = view;
    }

    public void setCustomerID(int customerID) {
       model.setID(customerID);
    }

    public int getCustomerID() {
        return model.getID();
    }

    public void setCustomerName(String customerName) {
       model.setName(customerName);
    }

    public String getCustomerName() {
        return model.getName();
    }

    public void setCustomerSurname(String customerSurname) {
       model.setSurname(customerSurname);
    }

    public String getCustomerSurname() {
        return model.getSurname();
    }

    public void displayCustomer() {        
        view.displayCustomer(model.getID(),model.getName(),model.getSurname());
    }

}

And this is class with main method, class MainCustomer:

package customereditor;

public class MainCustomer {

    public static void main(String[] args) {

        Customer model = new Customer();
        CustomerView view = new CustomerView();
        CustomerController controller = new CustomerController(model, view);        

        controller.setCustomerID(1);
        controller.setCustomerName("John");
        controller.setCustomerSurname("Doe");
        controller.displayCustomer();
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

What you have here is not MVC. It's DataVC, this results in some minor and major design problems.

Unwrapping data in the Controller

That's the first really bad idea here. You're trying to hide the way Data is structured from the View, based on the assumption that the Model is the Data.

That's not the case. Just pass your Customer-instance to the View for display. The view knows what it needs from the Customer to display it properly.
The Controller doesn't.

Model != POJO

The Model in an MVC application is not the "Domain Model". It's instead the Layer to provide instances of classes modelling that Domain Model (in your case Customers).

Instead of having the Controller receiving a Customer from the calling code (which is unsustainable in larger applications), you should make the Controller ask the Model for customers depending on user-input.

More nitpicks

  • Your package-declaration should hint at a domain you own.
  • Making the Customer's field accessible through getters on the Controller is a dumb idea. The Controller doesn't actually have to know about the internals of Customers and shouldn't be asked about them
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.