Правильная структура класса для обработчиков событий, чтобы избежать дублирования кода?JAVA

Программисты JAVA общаются здесь
Anonymous
Правильная структура класса для обработчиков событий, чтобы избежать дублирования кода?

Сообщение Anonymous »

Наша услуга, написанная на Java Processe Events. Первоначально мы получали только события типа Eventa Eventends Eventbase < /code>
, а наша служба обрабатывала события в: < /p>

Код: Выделить всё

public class EventAhandler {
public void handle(EventA a) {
EventInfo info = extractInfoFromEventBase(a)
GenericRequest x = convertToGenericRequestForEventA(info);
someProcessingOfGenericRequest(x)
}
}
Теперь мы также начнем получать события типа EventB Eventends EventBase
Фреймворк для событий требует, чтобы наша служба зарегистрировала другой обработчик для Eventa и Eventb .

Код: Выделить всё

public class EventBhandler {
public void handle(EventB b) {
EventInfo info = extractInfoFromEventBase(a)
GenericRequest x = convertToGenericRequestForEventB(info);
someProcessingOfGenericRequest(x)
}
}
< /code>
extractInfo
и некоторые процессоры в некотором процессе. Чтобы избежать дублирования кода, я сделал это: < /p>
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

Вернуться в «JAVA»