3
\$\begingroup\$

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 in the Software Engineering 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();
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ I have a feeling that your question might be better placed at SE Software Engineering (but maybe closed for a duplicate of what you mentioned). \$\endgroup\$ Dec 2 '19 at 19:36
  • \$\begingroup\$ Regarding the swing tag. Specifically a Controller class shouldn't need to know about anything of the GUI interfaces. \$\endgroup\$ Dec 2 '19 at 19:42
  • \$\begingroup\$ @πάνταῥεῖ ῥεῖ - What interacts with GUI interfaces then? \$\endgroup\$ Dec 2 '19 at 19:50

Your Answer

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

Browse other questions tagged or ask your own question.