I've got a problem while I'm trying to inherit a class which is followed by Singleton Pattern. Suppose there is a class, known as P, followed by Singleton Pattern and implementing an interface known as I_able.
abstract class P implement I_able {
static protected I_able instance = null;
protected object member1;
// abstract static I_able getInstance(); <-- this is an illegal declaration.
abstract public void method1() ;
} // class P
Now, there are two class wanting to inherit from P as following.
class A inheritance P {
static public I_able getInstance() {
if ( instance == null )
instance = new A() ;
return (A) instance ;
} // getInstance()
override public void method1() {
...
} // method1()
} // A()
class B inheritance P {
static public I_able getInstance() {
if ( instance == null )
instance = new B() ;
return (B) instance ;
} // getInstance()
override public void method1() {
...
} // method1()
} // B()
The problem is why the instance in A and instance in B are the same object. Actually, I kind of know why is that but I'm wondering how can I fix that. A and B must be the one and only object in the program. Any suggestion ?