Solved

Creating Objects which are Singleton wrt to a class variable

Posted on 2007-03-25
5
329 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 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