Solved

Creating Objects which are Singleton wrt to a class variable

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 159
learn programming 8 95
Java pass by reference 3 74
Which is best Image Resizing Web service 11 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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