mardi 3 octobre 2017

Strategy pattern with enum and lambdas design

I am trying to use Strategy pattern with enums and lambdas as follows

public enum InputType {

  UP {

     public IStrategy getStrategy() {
    return UP_STRATEGY;
     }

  },

  DOWN {
    public IStrategy getStartegy() {
        return DOWN_STRATEGY;
    }
  }


 public abstract IStrategy getStartegy();

 public static final IStrategy UP_STRATEGY = (n) -> {
                    for (int i =0; i<n; i++) {
                        System.out.println(i);
                    }
                };

 public static final IStrategy DOWN_STRATEGY = (n) -> {
                    //in my real implementation more lines of code. looks ugly to me                    
                    for (int i =n; i=0; i--) {
                        System.out.println(i);
                    }
                };
}

@FunctionalInterface
public interface IStrategy { 
    public void print(int n);
}

Is this a good design? having more lines of code in strategy constant is ok? any better design suggestions?

Aucun commentaire:

Enregistrer un commentaire