Skip to content

Instantly share code, notes, and snippets.

@manstis
Created January 26, 2018 12:23
Show Gist options
  • Select an option

  • Save manstis/1f8be2874f3aa0ec0e6ce7e757a20e38 to your computer and use it in GitHub Desktop.

Select an option

Save manstis/1f8be2874f3aa0ec0e6ce7e757a20e38 to your computer and use it in GitHub Desktop.
interface Command {
void execute();
}
interface DrawLineCommand extends Command { }
interface DrawBoxCommand extends Command { }
Command drawALine() {
return ()->doLineDrawing();
}
Command drawABox() {
return ()->doBoxDrawing();
}
I want to return an instance of DrawLineCommand from drawALine and DrawBoxCommand from drawABox
Obviusly I can not use lamdas, and just do new DrawLineCommand() { void execute() {...}}; etc
@tarilabs

Copy link
Copy Markdown

I would do it this way:

    static interface Command {

        void execute();
    }

    static interface DrawLineCommand extends Command {
    }

    static interface DrawBoxCommand extends Command {
    }

    static DrawLineCommand drawALine() {
        return () -> System.out.println("drawALine");
    }

    static DrawBoxCommand drawABox() {
        return () -> System.out.println("drawABox");
    }

    static List<Command> renderSomething() {
        List<Command> commands = new ArrayList<>();
        commands.add(drawALine());
        commands.add(drawABox());
        return commands;
    }

    @Test
    public void testMatteo() {
        for (Command command : renderSomething()) {
            System.out.println(command instanceof DrawLineCommand);
            System.out.println(command instanceof DrawBoxCommand);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment