A (deceptively) simple question: return Array, List or Iterator
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Victor_Volle
Posted On:   Sunday, December 1, 2002 10:00 AM

I have a Class that contains many Lists internally. How would you decide to make these Lists available: Just add getter (and setter) for the Lists itself add a getter but internally create a (cached) clone of the List return an Iterator on the List Create a (cached) Array of the real Type contained in the List. Some of the drivers for a decision might be    a) Do I want my clients to be able to alter my internal data (Yes: 1; No: 2;3;4)    b) Ease of implementation (1;3)    c) type safety (4)    d) standard Java API usage (java.lang.Class, java.a   More>>

I have a Class that contains many Lists internally.


How would you decide to make these Lists available:


  1. Just add getter (and setter) for the Lists
    itself
  2. add a getter but internally create a (cached)
    clone of the List
  3. return an Iterator on the List
  4. Create a (cached) Array of the real Type
    contained in the List.

Some of the drivers for a decision might be



   a) Do I want my clients to be able to alter my internal data (Yes: 1; No: 2;3;4)

   b) Ease of implementation (1;3)

   c) type safety (4)

   d) standard Java API usage (java.lang.Class, java.awt.List: 4; ...)


Victor

   <<Less

Re: A (deceptively) simple question: return Array, List or Iterator

Posted By:   Kiril_Dunn  
Posted On:   Monday, January 13, 2003 04:46 PM


Being a strong believer in hiding all implementation details from the user of one of my classes :), I would not do any of the above.


Rather, I'd probably add addElement(), removeElement() and getElement() methods for each list that you manage internally, or perhaps create my own implementation of Iterator to replace the getElement() and removeElement() methods.


By doing this, I have complete control over what the user of the class can and cannot do to my lists, and I can execute other code in response to adding, removing or retrieving an element from each list.


Cheers,


Kiril

About | Sitemap | Contact