2 Replies Latest reply: Oct 15, 2012 9:05 AM by 966867 RSS

    Custom Collections


      Ussually programmes declare collections as private and provide some custom methods to accsess them. The required logic is usually implemented in such methods.

      I belive the right way is to implement required logic directlly in custom-collection implemntation.
      public interface Shop{
           public Set<Customer> getCustomers();
           public Set<Product> getProducts();
      What is your opinion?
        • 1. Re: Custom Collections
          I'm inclined to agree with you, on the general grounds that applications generally contain if anything far too much code, but as always it depends. It depends for example on whether you want your collections to be read-only or read-write (although I guess if you want them read-only you could always return an unmodifiable wrapper), and on whether there are any other semantics associated with reading or writing the collection, and on whether it is sensible to grant access to multiple member collections separately considering they may leak into other threads for example.
          • 2. Re: Custom Collections
            Thank you for your reply,

            There culd be partlly unmodifieable collections which allows for example only to remove elements like java.util.Map.keySet();.The collections could validate added objects. As you immentioned different users should have different view to providen collections, etc

            I noticed, that in the praxis no one provide custom collection-implementation theire public APIs.

            Best Regards
            Andreas Hollmann