Java Lambda Command Pattern

I'm Sasaki from Media Development, Excite Japan Co., Ltd.

Introduction

It's a design pattern that everyone loves, but the Lambda expression introduced from Java 8 makes it easier to apply, so I'll introduce it now.

command pattern (conventional)

The command pattern is written as follows. File Action interface

FileAction.java


public interface FileAction {
    void openFile();
    void writeFile();
    void closeFile();
}

Command.java


public interface Command {
    void execute();
}

OpenFile.java


public class OpenFile implements Command {
    private final FileAction FileAction;
 
    public OpenFile(FileAction fileAction) {
        this.fileAction = fileAction;
    }
 
    @Override
    public void execute() {
        fileAction.openFile();
    }
}

CloseFile.java


public class CloseFile implements Command {
    private final FileAction FileAction;
 
    public CloseFile(FileAction fileAction) {
        this.fileAction = fileAction;
    }
 
    @Override
    public void execute() {
        fileAction.closeFile();
    }
}

WriteFile.java


public class WriteFile implements Command {
    private final FileAction FileAction;
 
    public WriteFile(FileAction fileAction) {
        this.fileAction = fileAction;
    }
 
    @Override
    public void execute() {
        fileAction.writeFile();
    }
}

Macro.java


public class Macro {
    private final List<Runnable> commands;

    public Macro(){
        this.commands = new ArrayList<>();
    }

    public void record(Command command) {
        this.commands.add(command);
    }

    public void run(){
        this.commands.forEach(e -> {
            e.execute();
        });
    }
}

I only issue three commands, but I have to implement the Command interface for each, which is quite annoying. If you partially rewrite this with Java: Lambda, it will be refreshing.

command pattern (Lambda expression)

FileActionImpl.java


public class FileActionImpl implements FileAction {

    @Override
    public void openFile() {
        System.out.println("open");
    }

    public void writeFile() {
        System.out.println("write");
    }

    @Override
    public void closeFile() {
        System.out.println("close");
    }
}

Create a class that implements the FileAction interface and collect methods there. (Here, it is only standard output for easy understanding)


    Macro macro = new Macro();
    FileActionImpl action = new FileActionImpl();
    macro.record(() -> action.openFile());
    macro.record(() -> action.writeFile());
    macro.record(() -> action.closeFile());
    macro.execute();

As above, write what you want the caller to do with each Lambda expression. Now, it will be executed when execute is executed.

It doesn't look like one operation and one file, so I think it's convenient when you want to save labor.

Finally

It's been 6 years since Java 8 was released. It's fast. Surprisingly, I don't see many design patterns optimized for Lambda expressions, so I wrote it.

Excite is looking for an engineer. We are looking forward to hearing from you. https://www.wantedly.com/projects/529139

Have a nice year !!

Recommended Posts

Java Lambda Command Pattern
Command Pattern
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
[Java] Strategy pattern
Java design pattern
java callback pattern
[Java] Singleton pattern
Design pattern ~ Command ~
[Java] Adapter pattern
Java lambda expression
Java pattern memo
My DAO pattern (Java)
java jar classpath command
java neutral lambda expression 1
Quarkus saves Java Lambda! ??
Understand Java 8 lambda expressions
Java lambda expression variations
java lambda expression memo
About Java lambda expressions
Java lambda expression [memo]
Explain Java 8 lambda expressions
Studying Java 8 (lambda expression)
Introduction to java command
Review java8 ~ Lambda expression ~
Java lambda expression again
Java design pattern summary
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Use Lambda Layers with Java
[Java] Functional interface / lambda expression
[Design pattern] Java core library
Java8 stream, lambda expression summary
Enum Strategy pattern in Java
[Java] Draw a simple pattern
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Java lambda expressions learned with Comparator
[Introduction to Java] About lambda expressions
Compile Java at the command prompt
Beginner's word No4 (Java command, JVM)
OpenJDK 8 java and javac command help
Is Java on AWS Lambda slow?
Hello World on AWS Lambda + Java
Java basic learning content 9 (lambda expression)
javac, jar, java command cheat sheet
Java
JNA (Java Native Access) pattern collection
Getting started with Java lambda expressions
What is a lambda expression (Java)
Java
Java beginner design pattern (Factory Method pattern)
The origin of Java lambda expressions
How to use Java lambda expressions
Implement API Gateway Lambda Authorizer in Java Lambda
Now let's recap the Java lambda expression
I tried to summarize Java lambda expressions
Nowadays Java lambda expressions and Stream API
[Java] for Each and sorted in Lambda
AWS Lambda with Java starting now Part 1
Working with huge JSON in Java Lambda