1

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?

2

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.

| improve this answer | |
  • You said that my controller class is fairly consistent with a View, so what does an actual controller does if not the thing I wrote?c And tank you for your response! – Zoran Jankov Dec 10 '19 at 9:12
  • @ZoranJankov, It seems you misunderstood the last sentence of my answer. Your Controller class is fairly typical for a system that has dumb View components. – Bart van Ingen Schenau Dec 10 '19 at 9:53
  • Aha, yes I have misunderstood you. 😃 – Zoran Jankov Dec 10 '19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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