The moose likes Beginning Java and the fly likes Encapsulating collection membership Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Encapsulating collection membership" Watch "Encapsulating collection membership" New topic
Author

Encapsulating collection membership

Malcolm Storey
Ranch Hand

Joined: Nov 09, 2008
Posts: 31
In my multi-windows app, am going to need various collections of my own objects. Am wondering whether it is better to:
A. encapsulate membership in member object creation (via factory method - don't want premature exposure!)
B. let the creator handle membership

Encapsulation makes managing membership over new/dispose cycles much less error prone and also facilitates a Dispose() method to unhook the object from all Collections of which it is a member. (This would be through my own Collectable interface.)

This naturally leads to a hierarchy of collections (app<-windows<-page-<my_primitives) which would be recursively iterated on Dispose(). [Yes, this is over-kill - some of those collections would also be being Disposed, but it would make life easier for GC and not worth trying to programmatically avoid. Dispose() will do other things too.]

Is this good design or am I getting carried away by the fun of it all?
[ November 18, 2008: Message edited by: Malcolm Storey ]

Malcolm

I wouldn't join any Collection with an add() method that would accept me as a parameter
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Encapsulating collection membership
 
Similar Threads
if statement (using BlueJ) and equalsIgnoreCase
Removing from an array
Iterator add and it.remove();
How I Implemented Membership Groups in Jforum
Tomcat 5.5.26 Sessions getting replicated but not working via browser