Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Isfirs opened this issue Mar 24, 2017 · 1 comment
Closed

Comments

@Isfirs
Copy link

Isfirs commented Mar 24, 2017

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
Copy link
Member

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants