Ben
Ben

Reputation: 2037

How to store references to all objects of a class?

Suppose there are two classes, ObjectA and ObjectB. Class ObjectA has a method to create objects of class ObjectB as follows:

private ObjectB createObjectB() {
    Object b = new ObjectB();
    return b;
 }

I want to keep track of all instances of ObjectB. What is a better way to do this?

What are the pros and cons of each method? Does it make a difference if I know that ObjectB objects will only ever be created from this one method in ObjectA?

Note that there will only ever be one instance of ObjectA, but the number of instances of ObjectB is variable.

Upvotes: 0

Views: 2622

Answers (6)

KidTempo
KidTempo

Reputation: 930

Based on what you said about ObjectC, I would say that it makes most sense for ObjectA (and C) to maintain an array of ObjectB. (Actually, I'd recommend using some sort of a Collection instead of an array if you expect to be adding and subtracting B's).

You can then create a methods to return the number of B's created by A e.g. A.countB()

If you find yourself in a position where you need the total number of B's created and you can't use A.countB()+C.countB(), then you have two options: a static map in B where the key is the creator object (A, C or whatever), and the value is the collection of B's.

I would discourage this for several reasons, mainly that objects created would hang around (so no GC, and potential memory issues).

The alternative is using a manager class for creating and keeping track of B and being very careful about dereferencing once you are finished with an instance.

Upvotes: 0

Naga
Naga

Reputation: 11

For me it looks like Object A and Object C are the classes which are managing the object B instance.

I have a similar scenario where I need to track different services which are sub classes of a Base service class (object B is the base class in my case).

To create/start/stop/restart/remove these services I have 2 manager classes for two different purposes (like object A and object C in your case).

In both manager classes I have a HashMap

You can have a method to stop/start/create/destory/remove B objects in class A or C which takes the 'name' ob object B and initiate corresponding operation on object B.

And as a best practice, in your SessionContextListener always make sure to call the stop/remove operation to remove the objects that are added in this HashMap.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328556

That depends on what you need to achieve. Should the instances of ObjectB (which is a bad name, btw) be able to talk to each other? If so, then you'll need to either create a global variable in the class (i.e. a static field) or you must keep the list in ObjectA and "inject" it into each instance of ObjectB - which means you need to keep a reference in both places.

If the instances of ObjectB don't need to talk to each other, it's usually enough to keep the list in ObjectA.

But most often, it's not necessary to keep a list at all. It can even be dangerous since you must make sure that the list doesn't grow until it consumes all memory.

The general rule is: Avoid unnecessary dependencies. By keeping a list, you're introducing a dependency which has a cost (for example, in maintenance plus you need to write the code in the first place).

Be ever wary of the cost of something you write - it's very easy to let these cheap things accumulate into something that is really expensive mountain of dependencies which strangle you.

Upvotes: 0

Attila
Attila

Reputation: 28762

You only need to "keep track" of object created that way if you want to access to all created objects from an unrelated part of the code. Otherwise 1) the created object will be held by variables, so you can access it and 2) when there are no more references to the object (so it is unreachable), the garbage-collector will clean the memory up eventually.

If you do keep track of them, keep in mind that the GC cannot release the memory while you have the object in the collection, which can lead to out-of-memory errors

Upvotes: 1

Orn Kristjansson
Orn Kristjansson

Reputation: 3485

It wouldn't make sense to hold a list in ObjectB, but a list in ObjectA would make sense if you need a reference to all the B objects you created.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120178

IF there is only one ObjectA instance, and it has a createB method, I would keep the list on the ObjectA instance itself. But in reality I don't think it matters.

public ObjectB createB(){
   Object B = new ObjectB();
   allBs.add(b); // assuming some sort of List
   return b;
}

I'm curious to know why you want to do this. You are going to potentially have threading issues, among other things, depending on your app and its architecture.

Upvotes: 0

Related Questions