aspose file tools
The moose likes Java in General and the fly likes Object referenced multiple collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Object referenced multiple collections" Watch "Object referenced multiple collections" New topic
Author

Object referenced multiple collections

TR Smith
Greenhorn

Joined: May 12, 2005
Posts: 9
In my application, I'm creating an object that may be referenced in multiple locations (added into multiple Collections). Change it once and it is changed for all the different references (correct functionality - everyone sees the change).

What I need is the ability to delete the object (remove all the references in the assorted collections, etc..). Is there some design pattern or methodology that can help me accomplish this?

Thanks for any help!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Don't think so.

You might give everyone a reference to a shell that has a private instance of the real thing. Then you'd only have to null out the reference in the shell to unlink all references to the real thing. All the other collections could reference the shell but they'd find it in an invalid state of some kind.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeremy French
Greenhorn

Joined: May 11, 2005
Posts: 13
You could create a listener-type structure, assuming you control all the Collection classes - i.e. they are subclasses, not the native ones. Then adding your deleteable object to the collection would at the same time register the Collection object with the deleteable object. Then when you need to delete the object, loop through your registered listeners and request they nullify their reference to you.

At least I think that would work...


There are 10 kinds of people in the world. Those that read binary and those that don't.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Fake names and "handles" aren't acceptable here. You can change your display name here. Thanks!


[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
What about the Observer Pattern:


The Observer pattern defines an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically.
The Observer pattern is also known as Dependents and Publish-Subscribe


The java.util package has an implementation.

Here is a link to tha java alamanac code samples:

Java Almanc
[ May 14, 2005: Message edited by: Edwin Dalorzo ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Object referenced multiple collections
 
Similar Threads
Objects eligible for garbage collection
References to an object in multiple collections.
collections
question concerning clone() method
Help with garbage collection and references!