Java 在命令模式中使用lambdas

Java 在命令模式中使用lambdas,java,design-patterns,lambda,Java,Design Patterns,Lambda,我希望了解如何使现有的命令模式实现适应Java8Lambdas @FunctionalInterface public interface Command { public void execute(final Vehicle vehicle); } public class LeftCommand implements Command { public void execute(final Vehicle vehicle){ vehicle.turnLeft(); }

我希望了解如何使现有的命令模式实现适应Java8Lambdas

@FunctionalInterface
public interface Command {

    public void execute(final Vehicle vehicle);
}

public class LeftCommand implements Command {

public void execute(final Vehicle vehicle){
    vehicle.turnLeft();
}

}

public class RightCommand implements Command {

public void execute(final Vehicle vehicle){
    vehicle.turnRight();
}

}
我有一个类VehicleManager,它调用processValue,该类根据作为inputValue传递给它的字符串L或R创建命令

processValues(Vehicle vehicle, String inputValue){
if("L".equals(inputValue){
  //create left command here
  Command cmd = (vehicle) -> vehicle.turnLeft(); //ERROR
  Command cmd = () -> vehicle.turnLeft(); //ERROR,expects 1 parameter vehicle to be passed
 }else{
 // create right command here
  Command cmd = (vehicle) -> vehicle.turnRight(); //ERROR
 }
}
我尝试如上所述使用lambdas创建命令,但它错误地指出车辆已定义

  • 您能告诉我如何使用lambdas在这里创建左命令和右命令实例吗

  • 如果我可以成功地使用上面的lambdas,那么我可以取消我的LeftCommand和RightCommand类吗

  • (我在谷歌上查了很多链接,但我没能做到这一点)

    在这篇文章中添加了一些评论

     private void processValues(String line, Vehicle vehicle) {
        List<Command> commands = new ArrayList<>();
        for(char c: line.toCharArray()){
            if(LEFT.equals(c)){
                commands.add(()-> vehicle.turnLeft());
            }else if(RIGHT.equals(c)){
                commands.add(()-> vehicle.turnRight());
            }else if(MOVE.equals(c)){
                commands.add(()-> rover.moveForward());
            }
        }
        commands.forEach((c) -> c.execute());
    }
    
    private void processvalue(字符串行、车辆){
    List命令=new ArrayList();
    for(char c:line.toCharArray()){
    if(左等于(c)){
    添加(()->vehicle.turnlight());
    }else如果(对,等于(c)){
    commands.add(()->vehicle.turnRight());
    }else如果(移动等于(c)){
    commands.add(()->rover.moveForward());
    }
    }
    commands.forEach((c)->c.execute());
    }
    

    这是正确的吗

    在命令模式中使用lambda或方法引用将使
    RightCommand
    LeftCommand
    类无用

    在您的第一个示例中,应该应用于以下内容:

    private void processValues(Vehicle vehicle, String inputValue) {
      Command command;
      if ("Left".equals(inputValue)) {
        command = v -> v.turnLeft();  // <- With lambda expresion
      } else {
        command = Vehicle::turnRight; // <- With method reference
      }
      command.execute(vehicle);
    }
    
    private void进程值(车辆、字符串输入值){
    指挥部;
    如果(“左”。等于(输入值)){
    
    command=v->v.turnlight();//在命令模式中使用lambda或方法引用将使您的
    RightCommand
    LeftCommand
    类无用

    在您的第一个示例中,应该应用于以下内容:

    private void processValues(Vehicle vehicle, String inputValue) {
      Command command;
      if ("Left".equals(inputValue)) {
        command = v -> v.turnLeft();  // <- With lambda expresion
      } else {
        command = Vehicle::turnRight; // <- With method reference
      }
      command.execute(vehicle);
    }
    
    private void进程值(车辆、字符串输入值){
    指挥部;
    如果(“左”。等于(输入值)){
    
    command=v->v.turnlight();//括号中的东西,
    (车辆)
    是一个新的变量声明,它是目标函数接口方法的参数。由于该上下文中已存在具有该名称的变量,因此不能在lambda表达式参数列表中使用它。请使用其他方法,例如
    v
    。您的
    processValues
    方法令人困惑:您不需要指定ic车辆创建命令…因此命令可以是这样的:
    Command cmd=v->v.turnlight();
    Command cmd=vehicle::turnlight
    。然后您可以将其应用于特定车辆:
    cmd.execute(车辆);
    ..@assylas我创建了类似的东西。这是正确的。请检查我在文章中添加的最后一节。括号之间的东西,
    (车辆)
    是一个新的变量声明,它是目标函数接口方法的参数。由于该上下文中已存在具有该名称的变量,因此不能在lambda表达式参数列表中使用它。请使用其他方法,例如
    v
    。您的
    processValues
    方法令人困惑:您不需要指定ic车辆创建命令…因此命令可以是这样的:
    Command cmd=v->v.turnlight();
    Command cmd=vehicle::turnlight
    。然后您可以将其应用于特定车辆:
    cmd.execute(车辆);
    …@assylias我创建了类似的东西。这是正确的吗。请检查我添加到我的文章的最后一部分。