Solved

Creating Objects which are Singleton wrt to a class variable

Posted on 2007-03-25
5
326 Views
Last Modified: 2013-12-29
Hi Experts,
i have a class say,
class Child extends Parent{
public static Hashtable allChilds ;
public Child ( String name)
{
  super ( name ) ;
}
}

here allChilds wil have all the children,

---------------------->

after each child is created that will be put in the allchilds,
i want childs with only unique names,
I mean the Child class should be singleton with respect to name, so if i try to instantiate a Child with a name which is already existing in the allChilds table, then newInstance of child should not be created .. the existing instance in the allChilds table's reference should be assigned to the new reference variable..

now the problem is i cannot use a factory method ( like getInstance(name) etc..) i want to do it only with the constructor as shown above. is it possible??

Thanks,
Mukundha
0
Comment
Question by:mukundha_expert
5 Comments
 
LVL 8

Accepted Solution

by:
mnrz earned 125 total points
ID: 18791089
so in constructor you can check it:

if( ! allChild.containsKey((String)name)){
    allChild.put(name,new Child())
}
0
 
LVL 11

Assisted Solution

by:elfe69
elfe69 earned 125 total points
ID: 18791096
You should definitely use a factory method. You will not be able to do what you want with a constructor anyway. So use a static getInstance(String) method.
0
 
LVL 10

Assisted Solution

by:Jaax
Jaax earned 125 total points
ID: 18791203
As elfe69 has said, it is not possible to implement using a public constructor. Making the constructor of the singleton defeats the purpose, as the mere invocation of the constructor creates another instance - forget abt restricting the instance using code within the constructor.

Also the method that returns the instance shoul be synchronized
Here is a sample implementation
public class Singleton {
    private Singleton() {
    }
    private static HashMap cache = new HashMap();

    public synchronized Singleton getSingleton(String key) {
        Singleton inst;
        if (cache.containsKey(key)) {
            inst = (Singleton) cache.get(key);
        } else {
            inst = new Singleton();
            cache.put(key, inst);
        }
        return inst;
    }
}
0
 
LVL 12

Assisted Solution

by:keyurkarnik
keyurkarnik earned 125 total points
ID: 18791726
There is no way you can achieve that with the constructor as mentioned by elfe and jaax.

You can either use static methods for it, or you could write a Factory class which will do the same work for you.
0
 
LVL 10

Author Comment

by:mukundha_expert
ID: 18791811
I just wanted to make sure that there is no other way.
Also throwing an exception from the constructor prevents the Object being succesfully instantiated.. so i was in doubt if there might be a way to actually refer to someother instance in memory//

anyways thanks a lot,

Cheers,
Mukundh
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now