EsotericSoftware / kryonet

TCP/UDP client/server library for Java, based on Kryo
BSD 3-Clause "New" or "Revised" License
1.81k stars 415 forks source link

Listener#doesAccept(Object) to prevent endless list of instanceof checks? #130

Closed Isfirs closed 6 years ago

Isfirs commented 7 years ago

Isn't it a better way to ask the listener if it may accept the Object by calling a Listener#doesAccept(Object):bool or Listener#doesAccept(Class):bool? (I don't care the actual name, but I prefer the class variant).

NathanSweet commented 6 years ago

No, such a method would have to do the logic to see if it can accept the object, return a boolean, then received would be called and that same logic would need to be done again. A listener does not need to use a list of instanceof checks, it can use a HashSet or any other technique it wants. If you have a large number of types, you could use a wrapper with an int or enum, eg:

switch (wrapper.type) {
case something:
    YourClass object = (YourClass)wrapper.object;
    break;
}