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メソッドをリファクタリングするために探しているので、任意のヘルプが歓迎されるだろう。あなたがコメントを頼んでクラスを更新しました。
完全にコンパイル可能な例はありませんが、あなたを助けるのは非常に困難です。私は 'cat.ifPresent(c - > {...})についてあなたに何かを伝えるかもしれません。私は最初にコンパイルしていないコードを投稿したくありません。エラーのリスクは大きすぎます。 –
更新されました – user1224036
なぜCatServiceは食べ物ですか?なぜ 'CurrentState'は' findByOutcomeId'の呼び出し元によって自身の 'outcomeId'を自分自身に渡す必要がありますか?そして、なぜそれらのサービスコールをそれぞれ関数に抽象化し、次にcomposeとandThenを使いたいのですか?あなたはその行動から何を期待していますか? – Holger