• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with ArrayLists  RSS feed

 
Ricardo Soares
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

what is the best way to make an ArrayList visible / accessible by various classes ? some of the classes are not inheriting so can't be done trough extending

is it correct to declare the ArrayList inside an interface and then implement it on the classes that are gonna access the ArrayList ?
example:



Thanks in advance
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ricardo Soares wrote: . . . is it correct to declare the ArrayList inside an interface and then implement it on the classes that are gonna access the ArrayList ?
example:



Thanks in advance
No, that is not correct. That looks like the constant interface anti‑pattern to me. If you want the list publically available, make it such via a public methodThe crafty thing about that code is that you return the List in a read‑only format. You can only add or remove in the original location.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem of your first approach is that every class implementing that interface will have its own [static] List, so you will not be sharing a List at all.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!