2016-09-16 5 views
1

Java 8を学習しようとしていますが、Cat、Food、CurrentState、OutcomeドメインオブジェクトとcatService、foodService、outcomeServiceがあります。だから、私の方法は、私は関数に抽象的にそれらのサービスコールのそれぞれを試してみましたが、その後、作曲とandThenが、それは仕事やその正しい方法かどうかはわからない使用このJava 8のサービス呼び出しと機能構成

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); 
       } 


      } 
     } 
    } 

} 

のように見えます。ので、私はprocessCatCanEatFoodEventメソッドをリファクタリングするために探しているので、任意のヘルプが歓迎されるだろう。あなたがコメントを頼んでクラスを更新しました。

+1

完全にコンパイル可能な例はありませんが、あなたを助けるのは非常に困難です。私は 'cat.ifPresent(c - > {...})についてあなたに何かを伝えるかもしれません。私は最初にコンパイルしていないコードを投稿したくありません。エラーのリスクは大きすぎます。 –

+0

更新されました – user1224036

+0

なぜCatServiceは食べ物ですか?なぜ 'CurrentState'は' findByOutcomeId'の呼び出し元によって自身の 'outcomeId'を自分自身に渡す必要がありますか?そして、なぜそれらのサービスコールをそれぞれ関数に抽象化し、次にcomposeとandThenを使いたいのですか?あなたはその行動から何を期待していますか? – Holger

答えて

1

Optionalについて学習した経験則は、「never use 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()andThen()。あなたが主張するならば、例えば:

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

努力する価値はありませんか?

関連する問題