• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting carried away with generics  RSS feed

 
Luke Murphy
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so here we go,

Say you have an interface which gets various types of objects:



But all these balls inherit from ball.

So could you just have




What is best practise?

Thanks so much.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends. If you want to add basket balls, cricket balls, golf balls etc then the second approach has the advantage that you don't have to add more methods. The implementing class may not be so lucky. Or it may, depending on the implementation. If you store all balls in one large collection then you can use the Class argument to filter on it (e.g. using the isInstance() method). If you store one collection per type then you'll have to add else statements for the new supported classes. I would be tempted to use the compact version though.

Oh, and it should be After all, Class is generic as well. But I think that's what you meant.
 
Luke Murphy
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:It depends. If you want to add basket balls, cricket balls, golf balls etc then the second approach has the advantage that you don't have to add more methods. The implementing class may not be so lucky. Or it may, depending on the implementation. If you store all balls in one large collection then you can use the Class argument to filter on it (e.g. using the isInstance() method). If you store one collection per type then you'll have to add else statements for the new supported classes. I would be tempted to use the compact version though.

Oh, and it should be After all, Class is generic as well. But I think that's what you meant.

Ye HA - I'd agree with that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!