dcsimg
Subclassing Singleton pattern
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   HELIGON_Sandra
Posted On:   Friday, December 19, 2003 05:40 AM

Hi, I have a class that implements "Singleton Pattern", I need to subclass this base class. I found a lot of example on the net but it does not work for my application. The class allows to save preferences, but the preferences are not the same for all the applications of the project. That is why I need to have several classes. The main class is the following: public class MyBaseClass { private File m_prefsFile; private static Hashtable m_registry = new Hashtable(); private static MyBaseClass s_instance = null; protected MyBaseClass() { File file = new File("Base.xml"); setFile(file); // access to    More>>

Hi,

I have a class that implements "Singleton Pattern",
I need to subclass this base class.
I found a lot of example on the net but it does
not work for my application.
The class allows to save preferences, but the preferences
are not the same for all the applications of the project.
That is why I need to have several classes.

The main class is the following:
public class MyBaseClass
{
private File m_prefsFile;
private static Hashtable m_registry = new Hashtable();
private static MyBaseClass s_instance = null;

protected MyBaseClass()
{
File file = new File("Base.xml");
setFile(file); // access to property m_prefsFile
m_registry("MyBaseClass", this);
}

public static MyBaseClass GetSingleInstance(String _name)
{
(MyBaseClass)m_registry.get(_name);
}
}

My problem is when I call the method GetSingleInstance
the return value is always null. With the debugger I never
pass in the constructor MyBaseClass (or subclass constructor).
Why ?

Thanks in advance
Sandra

   <<Less

Re: Subclassing Singleton pattern

Posted By:   Bhagyashree_Jayaram  
Posted On:   Tuesday, January 27, 2004 09:42 PM

Hi,

In the GetSinletonInstance() method you need to first check if the hash table i.e, m_registry contains the instance.If it does not contain then you need to call the constructor and then you need to call m_registry.get() method.

Since you are not doing it the object would never gets created and there is no call to the constructor.Hence the hash table never has the value.

Hope this helps...
Bhagya
About | Sitemap | Contact