0

I'm working on a non-generic server-client-architecture in Java. Currently, I face the issue of how to tackle Event-types since each event has different attributes, which don't overlap neccessarily. For now this is what I tried:

public interface Event {
   public void send(ServerConnection connection);
}

public static class Events {
   public class Registered implements Event {
       public final int clientId;
       public Registered(int clientId) {
          this.clientId = clientId;
       }
       public send(ServerConnection connection) {
           // ...
       }
   }
   public class Disconnected implements Event {
       public send(ServerConnection connection) {
           // ...
       }
   }
   public class Moved implements Event {
       public final int clientId;
       public final Position position;
       public Moved(int clientId, Position position) {
          this.clientId = clientId;
          this.position = position;
       }
       public send(ServerConnection connection) {
           // ...
       }
   }
   // and many more ....
}

However, this makes me quite unhappy, since I feel like repeating myself quite often. First, I was thinking of using an enumeration, but then I'd have unused values in there, since the attributes differ from event to event.

  • Welcome on SE. This could perhaps become an interesting question. However, unfortunately, it is really unclear what you're asking and what you expect from the community. Could you clarify ? – Christophe Aug 26 '17 at 17:55
  • @Christophe I tried to clarify. Hope it's better now – NaCl Aug 26 '17 at 18:11
  • This just looks like normal Java code to me … not really any avoidable repetition. Have you considered using a less verbose language? If absolutely necessary you could use annotations for code generation (→ Lombok) but that feels wrong to me. – amon Aug 26 '17 at 18:19
  • @amon I have to use Java. It's fun. :( – NaCl Aug 26 '17 at 18:24
  • @NaCL much better indeed ! – Christophe Aug 26 '17 at 19:32
2

This just looks like normal Java code to me … not really any avoidable repetition. Have you considered using a less verbose language, like Scala? Its case-classes might help.

With Java8 you get another option that may sometimes be useful. If you only need different Event#send() implementations but not different types, you could use a lambda instead:

@FunctionalInterface
public interface Event {
   public void send(ServerConnection connection);
}

public static class Events {
   public Event registered(int clientID) {
       return connection -> { ... };
   }
   public Event disconnected() {
       return connection -> { ... };
   }
   public Event moved(int clientID, Position position) {
       return connection -> { ... };
   }
   // and many more ....
}

In most scenarios that is sufficient, but you do lose a bit debuggability and the ability to check if (ev instanceof Events.Moved) etc.

  • +1 for suggesting the use of scala case classes. if OP really wanted they could even write the messages in scala and then do the rest in java. – Phoenix Aug 26 '17 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.