减少的 if else 嵌套 可以使用java 8的Consumer

private static Map> maps = new HashMap>();

static {
    //这里用到landa表达式,新特性。 其中 Cat,Dog 可以看成 if-else 中的条件
    maps.put("type1", productVO -> test1(productVO));
    maps.put("type2", productVO -> test2(productVO));
}

public static void main(String[] args) {
    Consumer cat = maps.get("Cat");
    ProductVO productVO = new ProductVO();
    productVO.setBrandName("sasdfs");
    cat.accept(productVO);
}

private static Boolean test1(ProductVO productVO) {
    System.out.println("RunCase2" + productVO.getBrandName());
    return true;
}

private static Boolean test2(ProductVO productVO) {
    System.out.println("RunCase1" + productVO.getBrandName());
    return false;
}

你可能感兴趣的:(减少的 if else 嵌套 可以使用java 8的Consumer)