In my application there are two ways events can be received - either from the background handled by the BackgroundEventListener
class or read in as a line from file handled by the FromFileEventListener
class. Let's assume they both implement the following interface:
public interface EventListener<E> {
void mousePressed(E mouseEvent);
void mouseReleased(E mouseEvent);
void mouseClicked(E mouseEvent);
}
The BackgroundEventListener
class:
public class BackgroundEventListener implements EventListener<MouseEvent> {
EventHandler<MouseEvent> eventHandler;
public BackgroundEventListener() {
eventHandler = new BackgroundEventHandler();
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
eventHandler.handleEvent(mouseEvent, "mousePressed");
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
eventHandler.handleEvent(mouseEvent, "mouseReleased");
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
eventHandler.handleEvent(mouseEvent, "mouseClicked");
}
}
We can see that the BackgroundEventListener
class passes in the type of the event directly depending on which of the methods was called.
EventHandler
interface and implementations:
public interface EventHandler<E> {
void handleEvent(E event, String type);
}
Background:
public class BackgroundEventHandler implements EventHandler<MouseEvent> {
@Override
public void handleEvent(MouseEvent event, String type) {
// TODO handle event coming from background
}
}
From file:
public class FromFileEventHandler implements EventHandler<String> {
@Override
public void handleEvent(String event, String type) {
// TODO handle event coming from background
}
}
Here comes my question. In the background case the event type has to explicitly be passed as an extra argument. However, when an event is read from file the information about the type of the event is present in the event itself. Therefore, the FromFileEventListener
class would need to look something like this:
public class FromFileEventListener implements EventListener<String> {
EventHandler<String> eventHandler;
public FromFileEventListener() {
eventHandler = new FromFileEventHandler();
}
@Override
public void mousePressed(String event) {
eventHandler.handleEvent(event, Utils.getTypeOutOfEvent(event));
}
@Override
public void mouseReleased(String event) {
eventHandler.handleEvent(event, Utils.getTypeOutOfEvent(event));
}
@Override
public void mouseClicked(String event) {
eventHandler.handleEvent(event, Utils.getTypeOutOfEvent(event));
}
}
This results in the event being passed in as the first argument and the second argument being the type which needs to be extracted beforehand, for example using a public static method getTypeOutOfEvent(String event)
of class Utils
- an obviously unnecessary duplication of data and splitting event processing between different classes.
What would be the elegant solution to that problem? What design pattern should I use? (One way I thought of was to create an EventWrapper
which I could use to wrap either a String event
or a MouseEvent event, String type
pair)
Aucun commentaire:
Enregistrer un commentaire