简体   繁体   中英

How can I avoid dependencies between methods?

I have the following method:

protected ArrayList<String> prepInstaller(RemoteSession session) {
    ArrayList<String> installCommand = new ArrayList<String>();
    installer.setInstallOption(getCommand());
    installer.setInstallProperties(generateInstallProperties());
    installer.setTestHome(getTestHome(session));

    switch (installer.getInstallOption()) {
    case ("addon"):
        installer.setAddOnScriptPath(getAddOnScriptPath(session, true));
        installer.setAddOnImageLocation(getAddOnPath(session, true));
        installCommand.add(installer.getInstallCommand());
        break;
    case ("install"):
        installer.setImageLocation(getImageLocation(session, true));
        installCommand.add(installer.getInstallCommand());
        break;
    case ("patch"):
    case ("rollback"):
        installer.setPatchLocationPath(getPatchPath(session, true));
        for(String currentPatch: installer.getPatches()) {
            installCommand.add(installer.getInstallCommand(currentPatch));
        }
        break;
    }

    return installCommand;
}

My problem is that this line:

installCommand.add(installer.getInstallCommand());

contains installer.getInstallCommand() which will contain null objects unless the following are run:

installer.setInstallOption(getCommand());
installer.setInstallProperties(generateInstallProperties());
installer.setTestHome(getTestHome(session));

among others... The method has dependencies on previous methods being run which is not desirable. I have defined installer as a static factory:

public static InstallData getInstance() {
    if (instance == null) {
        instance = new InstallData();
    }
    return instance;
}

I have looked at using the builder pattern but it seems a bit unwieldly. I couldn't find a tidy version of it. I don't want to use a constructor, as it will be messy and I will need several of them.

I have also tried to construct an object containing all of the set methods and then passing the object into a new class which returned getInstallCommand() , but that got quite messy also.

Ideas welcome :)

As it is pointed out in the question, it is not a desirable behaviour for an object to be instantiated or a method to be called while the object is in an illegal state. In most cases where an object creation requires 4 or more arguments but there can be reasonable defaults for some of them, the telescoping constructor (anti)pattern can be used by requiring the necessary arguments and substituting optional ones.

In other cases, especially if the arguments are of a similar/same type the builder pattern is used. Instead of specifying all parameters at once, they are set one by one and finally the Builder object is used to instantiate the desired class.

The question refers to "messy" code on a few occasions. While this is true for arbitrary code, a design pattern is

a general reusable solution to a commonly occurring problem within a given context in software design.

Hence, if properly implemented it either works for the use case or it doesn't but it shouldn't be "messy". I'd also recommend having a look at java implementations of commonly used design patterns . Perhaps, a more suitable alternative can be found that fits in the question domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM