Java 8服务调用和功能构造

时间:2016-09-16 08:21:23

标签: java functional-programming java-8

我正在尝试学习Java 8,我有Cat,Food,CurrentState,Outcome域对象和catService,foodService,outcomeService。所以我的方法看起来像这样

public class Cat {
    private Long ownerId;
    private Long Id;

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public CurrentState findActiveCurrentState() {
        return new CurrentState();
    }

}

class CurrentState {
    Long outcomeId;

    public Long getOutcomeId() {
        return outcomeId;
    }

    public void setOutcomeId(Long outcomeId) {
        this.outcomeId = outcomeId;
    }

    Outcome findByOutcomeId(Long outcomeId) {
        return new Outcome();
    }
}

class Food {

}

class Outcome {
    Long outcomeId;
    List<String> types;

    public List<String> getTypes() {
        types = new ArrayList<>();
        types.add("Food");
        types.add("Bath");
        return types;
    }

    public void setTypes(List<String> types) {
        this.types = types;
    }
}


class CatService {
    Optional<Cat> findByOwnerId(Long ownerId) {
        return Optional.of(new Cat());
    }

    public void eatFood(Food food) {

    }
}

class FoodService {
    Food find(Long catId) {
        return new Food();
    }

    class FoodEventService {

        private CatService catService = new CatService();

        private FoodService foodService = new FoodService();

        public void processCatCanEatFoodEvent(Long ownerId) {
            Optional<Cat> cat = catService.findByOwnerId(ownerId);
            if (cat.isPresent()) {
                //dont worry about the findActiveCurrentState(),its not really important
                CurrentState currentState = cat.get().findActiveCurrentState();
                Food food = foodService.find(cat.get().getId());
                Outcome outCome = currentState.findByOutcomeId(currentState.getOutcomeId());

                if (outCome.getTypes().contains("Food")) {
                    catService.eatFood(food);
                }


            }
        }
    }

}

我试图将每个服务调用抽象为函数,然后使用compose和andThen,但不确定它是否可行或正确。所以任何帮助将不胜感激,所以我期待重构processCatCanEatFoodEvent方法。正如你在评论中提到的更新了课程。

1 个答案:

答案 0 :(得分:1)

我已经了解Optional s的经验法则是“从不使用get()”。在你的情况下,因为你已经有了if语句,所以找到更优雅的东西并不难:

        cat.ifPresent(c -> {
            CurrentState currentState = c.findActiveCurrentState();
            Food food = foodService.find(c.getId());
            Outcome outCome = currentState.findByOutcomeId(currentState.getOutcomeId());

            if (outCome.getTypes().contains("Food")) {
                catService.eatFood(food);
            }
        });

对于你的问题,在玩了一下之后我倾向于同意Louis Wasserman,引入消费者compose()andThen()并没有真正的意义。如果你坚持,你可以做,例如:

            if (((Function<Outcome, List<String>>) Outcome::getTypes)
                    .andThen(l -> Boolean.valueOf(l.contains("food")))
                    .apply(outCome)) {
                catService.eatFood(food);
            }

不值得努力,是吗?