Hi. I’d like to use Singletons. Maybe the following singleton pattern class would do this:
(Addendum: doesn’t work, because of the static sInstance for any singleton inheritance)
abstract public class Singleton
{
private static Singleton sInstance;
public Singleton() {
assert sInstance == null;
sInstance = this;
}
public static Singleton getInstance() {
assert sInstance != null;
return sInstance;
}
}
Now let’s define a class which wants to be a singleton.
class Example extends Singleton
{
public void method() {
// bla
}
}
Finally let’s fetch the instance of the example class
{
// Once the example class is being created.
Example mMyexample = new Example();
}
{
// Later on, we want to access the singleton class
Example ex = (Example) Example.getInstanz(); // How to avoid the Cast?
ex.method();
}
Is it possible to avoid the cast operation?