java - Using Singleton Design with Inheritance in a multithreaded Application -
i having application code written this
public interface superclass{ //....to methods } public class baseclass1 implements superclass{ //....to methods } public class baseclass2 implements superclass{ //....to methods }
now in application 1 object can instantiated superclass reference, i.e synchronized singleton.
now, can include
public static synchronized superclass getinstance(){ //initialise }
in of base classes, cannot include declaration in superclass interface due cannot instance of base class using reference of super class. e.g. want this(its vague example)
baseclass1 bc1 = baseclass1.getinstance(); superclass sc = bc1.getinstance();
now somewhere later in code
bc1=sc.getinstance();
how can achieved?
note: several threads accessing these objects hence synchronisation mandatory
instead of using singletons why dont use dependency injection eg: google guice.
guice create instance of super class , can access via injector.getinstance() method.
Comments
Post a Comment