简体   繁体   中英

java class with different parameters as parameter of method

I have quite untypical question. In my app I have the following code:

public TaskListDatabases getTaskListDatabases (int mode) {
    switch (mode) {
        case (MODE_NORMAL): 
            if (taskListDatabases == null) {
                taskListDatabases = new TaskListDatabases(app); // 1st place
            }
            break;
        case (MODE_FORCE_RERUN): 
            if (taskListDatabases == null) {
                taskListDatabases = new TaskListDatabases(app); // 2nd place
            } else {
                if (taskListDatabases.getStatus() == AsyncTask.Status.FINISHED)
                    taskListDatabases = new TaskListDatabases(app); // 3rd place
            }
            break;
    }
    return taskListDatabases;
}

Now I would like to create 4 other functions similar to the one above but differing in the actual object construction place (marked with a comment).

In 1st instead of new TaskListDatabases(app) I would like to have new Task (app, int, int) , in the 2nd new Task (app, String, int) , in the 3rd new Task (app, String, String, String) ....

All the rest would be the same. I'm wondering if there is any chance to create one function taking as "parameter" the part new TaskListDatabases(app) . If it was only the question of class, it wouldn't be the problem as I would have "Class param". However here each class has also different constructor parameters.

Any idea?

What you want is an abstract factory implemented as an anonymous callback behind a facade . Confused? Hopefully won't be after this:

Define an interface for the anonymous callbacks:

private interface TaskFactory {
    Task create();
}

Refactor the base method to use a factory

private Task getTask (int mode, TaskFactory factory) {
    switch (mode) {
        case MODE_NORMAL: 
            if (taskListDatabases == null) {
                taskListDatabases = factory.create();
            }
            break;
        case (MODE_FORCE_RERUN): 
            if (taskListDatabases == null) {
                taskListDatabases = factory.create();
            } else {
                if (taskListDatabases.getStatus() == AsyncTask.Status.FINISHED)
                    taskListDatabases = factory.create();
            }
            break;
    }
    return taskListDatabases;
}

Then create facade methods for each purpose:

public Task getTaskListDatabases(int mode) {
    return getTask(mode,
        new TaskFactory() {
            Task create() {
                return new TaskListDatabases(app);
            }
        }
    }
}

public Task getTaskOther(int mode) {
    return getTask(mode,
        new TaskFactory() {
            Task create() {
                return new Task(app, "foo", 7; // eg
            }
        }
    }
}

// etc for other Task flavours

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