mercredi 17 février 2016

What pattern would solve this? trying to avoid procedural coding

I have a MVC system, where the V returns a user object, containing a username, password, name and so on,

One of the options in the V is to modify these values stored in a storage, so the person is asked for a few parameters, of what to change or what to keep.

the problem I am facing is in the M, where I have to check these values and validate them, my code is looking something like this,

if(username != "" && validate(username) && checIfExist(username) {
//Change username
}
if(password != "" && validate(password)) {
//Change password
}

And so on, the reason for != "" is to check whether or not the parameter wants to be modified, for example a user can change password or their name, and if they wish to do so they enter something so != "" would be false, and the program can continue to validate and change it.

but the problem here is there are many if statements, and it increases depending on if I decide to give the user more specific information, like city they live in, marriage status, date of birth, and so on.

so I am wondering how could I change this to make the code neater and cleaner to read, but still do what it should do.

This project has a Starter class, with a main function, which just runs the Controller class,

public static void main(final String[] args)
{
    Controller controller = new Controller();
    controller.runApplication();
}

The Controller class,

private IMenuManager menuManager = new MenuMananger();
private IUserMananger userManager = new UserManager();
private IUserDTO userInfo = new UserMapDTO();

private IUserMenuChoice userMenuChoice = new UserMenuChoice();
private ICommand executeChoice; //Implementing the command pattern

public final void runApplication()
{
    UserMenuDTO details = menuManager.showInitialMenu();
    while (userDTO.unpackInfo(details)) // Unpacks the information and stores it in a hashmap, will stop if the user decided to EXIT
    {
        executeChoice = userMenuChoice.getUserChoice(details.getUserChoice());
        details = menuManager.showInitialMenu();
    }
}

Aucun commentaire:

Enregistrer un commentaire