import lombok.Getter;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
class Scratch {
public enum Animal {
HIPPO("Grass", BaseWorker::feedHippo),
PENGUIN("Fish", BaseWorker::feedPenguin),
MONKEY("Banana", BaseWorker::feedMonkey),
OWL("Mouse", BaseWorker::feedOwl);
@Getter
private final String foodName;
@Getter
private final BiConsumer<BaseWorker, Integer> foodCalculation;
Animal(String foodName, BiConsumer<BaseWorker, Integer> foodCalculation) {
this.foodName = foodName;
this.foodCalculation = foodCalculation;
}
}
//@Component - воркер может быть под управлением DI контейнера
static class BaseWorker {
//@Autowired - с инжектом внешних зависимостей.
MenuRepository menuRepository;
public void feedHippo(int animalCount) {
//Сложная логика
int foodQuantity = (int) Math.pow(animalCount, 2);
System.out.printf("Hippo eat: %d %s", foodQuantity, menuRepository.findMenuFor("Hippo"));
}
void feedPenguin(int animalCount) {
//Сложная логика
int foodQuantity = (int) (Math.pow(animalCount, 3) / 2);
System.out.printf("Penguin eat: %d %s", foodQuantity, menuRepository.findMenuFor("Penguin"));
}
void feedMonkey(int animalCount) {
//Сложная логика
int foodQuantity = animalCount * 10;
System.out.printf("Penguin eat: %d %s", foodQuantity, menuRepository.findMenuFor("Monkey"));
}
void feedOwl(int animalCount) {
//Сложная логика
int foodQuantity = animalCount * 3;
System.out.printf("Penguin eat: %d %s", foodQuantity, menuRepository.findMenuFor("Owl"));
}
}
}
Мне кажется, можно чутка дальше подход развить... Вариант более лаконичный в плане создания классов, позволяет инжектить зависимости и менее хардкордный в плане воркеров.
Здравствуйте, а когда планируются ранее обещанные книги: - Grokking Simplicity Taming complex software with functional thinking - Fundamentals of Software Architecture
Мне кажется, можно чутка дальше подход развить...
Вариант более лаконичный в плане создания классов, позволяет инжектить зависимости и менее хардкордный в плане воркеров.
Здравствуйте, а когда планируются ранее обещанные книги:
- Grokking Simplicity Taming complex software with functional thinking
- Fundamentals of Software Architecture
Жалко, что хорошую книгу, а перевели тяп-ляп машинным переводом. :(