, а наша служба обрабатывала события в: < /p>
Код: Выделить всё
public class EventAhandler {
public void handle(EventA a) {
EventInfo info = extractInfoFromEventBase(a)
GenericRequest x = convertToGenericRequestForEventA(info);
someProcessingOfGenericRequest(x)
}
}
Фреймворк для событий требует, чтобы наша служба зарегистрировала другой обработчик для Eventa и Eventb .
Код: Выделить всё
public class EventBhandler {
public void handle(EventB b) {
EventInfo info = extractInfoFromEventBase(a)
GenericRequest x = convertToGenericRequestForEventB(info);
someProcessingOfGenericRequest(x)
}
}
< /code>
extractInfo
public class CommonEventHandler {
public void handle(EventBase event, Function converter) {
EventInfo info = extractInfoFromEventBase(event)
x = converter.apply(info);
someProcessingOfGenericRequest(x)
}
}
public class EventAhandler {
public void handle(EventA a) {
commonEventHandler.handle(a, convertToGenericRequestForEventA);
}
}
public class EventBhandler {
public void handle(EventB b) {
commonEventHandler.handle(b, convertToGenericRequestForEventB);
}
}
< /code>
A code reviewer on our team said this way of passing functions is not very readable/maintainable and not very OOP-like (Esp in Java). They also think one of the better ways to do this would be to have a base abstract class and put the common code there.
I disagree and think this style is pretty readable. Any thoughts on whether this pattern is good and/or suggestions to do it some other way?
Подробнее здесь: https://stackoverflow.com/questions/795 ... uplication