Solved

Creating Objects which are Singleton wrt to a class variable

Posted on 2007-03-25
5
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

617 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