Java8服务调用和函数构造
我正在努力学习Java8,我有Cat、Food、CurrentState、结果域对象和catService、foodService、outcomeService。我的方法是这样的Java8服务调用和函数构造,java,functional-programming,java-8,Java,Functional Programming,Java 8,我正在努力学习Java8,我有Cat、Food、CurrentState、结果域对象和catService、foodService、outcomeService。我的方法是这样的 public class Cat { private Long ownerId; private Long Id; public Long getOwnerId() { return ownerId; } public void setOwnerId(Long
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);
}
}
}
}
}
公共类Cat{
私人长期所有权;
私人长Id;
公共长getOwnerId(){
返回所有者ID;
}
public void setOwnerId(Long ownerId){
this.ownerId=ownerId;
}
公共长getId(){
返回Id;
}
公共无效集合id(长id){
Id=Id;
}
public CurrentState findActiveCurrentState(){
返回新的CurrentState();
}
}
类当前状态{
长期结果;
公共长getOutcomeId(){
返回结果;
}
public void setOutcomeId(Long outcomeId){
this.outcomeId=outcomeId;
}
结果findByOutcomeId(长结果ID){
返回新结果();
}
}
等级食品{
}
班级成绩{
长期结果;
列表类型;
公共列表getTypes(){
类型=新的ArrayList();
类型。添加(“食品”);
类型。添加(“浴缸”);
返回类型;
}
公共void集合类型(列表类型){
this.types=类型;
}
}
类别CatService{
可选findByOwnerId(长ownerId){
返回可选的.of(新的Cat());
}
公共食品(食品){
}
}
等级餐饮服务{
食物发现(长catId){
归还新食物();
}
类FoodEventService{
专用CatService CatService=新CatService();
私有FoodService FoodService=新FoodService();
public void processCatCanEatFoodEvent(长所有者ID){
可选cat=catService.findByOwnerId(ownerId);
if(cat.isPresent()){
//不要担心findActiveCurrentState(),它不是很重要
CurrentState CurrentState=cat.get().findActiveCurrentState();
Food=foodService.find(cat.get().getId());
结果=currentState.findByOutcomeId(currentState.getOutcomeId());
if(output.getTypes()包含(“食物”)){
catService.eatFood(食品);
}
}
}
}
}
我试图将这些服务调用中的每一个抽象为一个函数,然后使用compose和and,但不确定它是否会工作或是否正确。因此,任何帮助都将不胜感激,所以我希望重构processCatCanEatFoodEvent方法。正如您在评论中所问的那样,更新了该类。我学到的关于
可选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);
}
});
对于你的问题,在经过一番讨论之后,我倾向于同意路易斯·沃瑟曼的观点,介绍消费者,compose()
和and()
没有真正的意义。如果您坚持,您可以这样做,例如:
if (((Function<Outcome, List<String>>) Outcome::getTypes)
.andThen(l -> Boolean.valueOf(l.contains("food")))
.apply(outCome)) {
catService.eatFood(food);
}
if(((函数)结果::getTypes)
然后(l->Boolean.valueOf(l.contains(“food”))
(适用(结果)){
catService.eatFood(食品);
}
不值得这么做,是吗?没有一个完整的、可编译的示例,很难帮助您。我可能会告诉你一些关于'cat.ifPresent(c->{…});但我不想发布我没有首先编译的代码。出现错误的风险太大。请参见为什么CatService
在吃食物?为什么CurrentState
需要它自己的outcomeId
由findByOutcomeId
的调用者传回自己?为什么要“将每个服务调用抽象为一个函数,然后使用compose和And”?您对该操作的期望是什么?您的代码看起来非常好,如果您的代码没有Java 8功能,那么您就没有必要将其推到Java 8功能上。