I am learning OOP, concretely Java, by developing a real life business application for aimed for repair shops. I have followed MVC pattern design for GUI elements of my application. I was wondering do my input dialog controller classes has to much responsibilities. They set ComboBox models, button ActionListeners, verify user input, show input errors, and save data to model. Is that to much responsibilities, or is that actually a single responsibility, connecting the view with the model? I am aware of this topic on this website.

===========================================================================

This is my abstract input dialog controller class that is a super class for all input dialog controllers. It is responsible for setting the ID label value, setting Add and Cancel buttons ActionListeners, and has the main function of input controllers, saving data.

    public abstract class InputDialogController implements WindowController
    {
        protected DataType dataType;
        protected int id;
        protected InputDialog gui;

        protected InputDialogController(WindowController owner, DataType dataType)
        {
            this.dataType = dataType;
            gui = InputDialogFactory.getWindow(owner.getWindow(), dataType);
            id = IDGenerator.getNewID(dataType);
            gui.getIdPanel().setIdValue(IDGenerator.formatRegularID(id));
            gui.getInputButtonPanel().setBtnAddActionListener(ActionListenerFactory.saveData(this));
            gui.getInputButtonPanel().setBtnCancelActionListener(ActionListenerFactory.closeWindow(this));
        }

        @Override
        public Window getWindow()
        {
            return (Window) gui;
        }

        public void trySavingDataElement()
        {
            if(isInputValid())
            {
                DataManager.save(createDataElement());
                getWindow().dispose();
            }
            else
            {
                showInputErrors();
            }
        }

        protected abstract boolean isInputValid();

        protected abstract DataElement createDataElement();

        protected abstract void showInputErrors();
    }

This is one of mine concrete input dialog controllers, the ClientRegistrationController class.

public class ClientRegistrationController extends InputDialogController
{
    private ClientRegistrationDialog clientGUI;

    public ClientRegistrationController(WindowController owner, DataType dataType)
    {
        super(owner, dataType);
        clientGUI = (ClientRegistrationDialog) super.gui;
        clientGUI.getMarketingPanel().setMarketingCmbModel(CmbModelFactory.getModel(dataType));
        clientGUI.getMarketingPanel()
                 .setBtnMarketingActionListener(ActionListenerFactory
                 .openNewWindow(this, DataType.MARKETING_TYPE));

    }

    public void updateComboBoxes(String item)
    {
        clientGUI.getMarketingPanel().setMarketing(item);
    }

    @Override
    protected boolean isInputValid()
    {
        return isNameValid()
            && isPhoneNumberValid()
            && isMarketingSelected();
    }

    private boolean isNameValid( )
    {
        return !("".equals(clientGUI.getPersonalInfoPanel().getName()));
    }

    private boolean isPhoneNumberValid()
    {
        String phoneNumber = clientGUI.getPersonalInfoPanel().getPrimePhoneNumber();

        return !(DataManager.clientsDataTable.uniqueStringCollision(phoneNumber)
             || ("".equals(phoneNumber)));
    }

    private boolean isMarketingSelected()
    {
        return clientGUI.getMarketingPanel().getMarketing() != "";
    }

    @Override
    protected Client createDataElement()
    {
        Client newClient= new Client();

        newClient.setId(id);
        newClient.setName(clientGUI.getPersonalInfoPanel().getName());
        newClient.setPrimePhoneNumber(clientGUI.getPersonalInfoPanel().getPrimePhoneNumber());
        newClient.setAlternativePhoneNumber(clientGUI.getPersonalInfoPanel().getAltPoneNumber());
        newClient.setEmail(clientGUI.getPersonalInfoPanel().getEmail());
        newClient.setAddress(clientGUI.getPersonalInfoPanel().getAddress());
        newClient.setMarketing(DataElementGetter.getMarketing(clientGUI.getMarketingPanel().getMarketing()));

        return newClient;
    }

    @Override
    protected void showInputErrors()
    {
        checkName();
        checkPhoneNumber();
        checkMarketing();
    }

    private void checkName()
    {
        if(isNameValid())
        {
            clientGUI.getPersonalInfoPanel().showNameDefault();
        }
        else
        {
            clientGUI.getPersonalInfoPanel().showNameError();
        }
    }


    private void checkPhoneNumber()
    {

        if(isPhoneNumberValid())
        {
            clientGUI.getPersonalInfoPanel().showPhoneDefault();
        }
        else
        {
            clientGUI.getPersonalInfoPanel().showPhoneError();
        }
    }

    private void checkMarketing()
    {
        if(isMarketingSelected())
        {
            clientGUI.getMarketingPanel().showMarketingDefault();
        }
        else
        {
            clientGUI.getMarketingPanel().showMarketingError();
        }
    }
}

Am I implementing the MVC pattern the wrong way, and how it should be done?

有帮助吗?

解决方案

There are multiple correct ways to implement/use the MVC pattern, in part depending on the technology you use and personal preferences.

The most important aspect of the MVC pattern is that all the user-interaction related code is confined to the Controller and/or View portions. In other words, it should be possible to create a completely different UI (for example, a web-ui) without making any modification to the Model part.

As for which code should go into the Controller and which into the View depends to a large extent on how comfortable you and the designers of the GUI framework you use are with having logic in the View portion. The controller code you presented is fairly consistent with a Controller that is paired to a View that contains little logic and is entirely valid.

许可以下: CC-BY-SA归因
scroll top