Singleton subclass

I have an abstract base class and an implementation class like:

public abstract class Base
{
   public Base getInstance( Class<? extends Base> clazz )
   {
      //expected to return a singleton instance of clazz's class
   }

   public abstract absMeth();
}

public A extends Base
{
    //expected to be a singleton
}

In this example I can make A to be a singleton and even write getInstance in Base to return a singleton object of A for every call, doing this way:

public abstract class Base
{
   public Base getInstance( Class<? extends Base> clazz )
   {
      try
      {
         return clazz.getDeclaredMethod("getInstance").invoke(null,null);
      }
   }

   public abstract void absMeth();
}

public A extends Base
{
    private static A inst;

    private A(){}

    public static A getInstance( )
    {
       if( inst!= null)
            inst = new A();
       return inst; 
    }

    public void absMeth(){
      //...
    }
}

But my concern is how do I ensure that if somebody writes another class class B extends Base it should also be a singleton and it necessarily implements a static method called getInstance?

In other words I need to enforce this as a specification for all classes extending with the Base class.

Answers


You cannot trust classes that extend you to create a single instance of themselves1: even if you could somehow ensure that they all implement getInstance, there is no way to tell that inside that method they check inst before constructing a new instance of themselves.

Stay in control of the process: create a Map<Class,Base>, and instantiate the class passed in through reflection2. Now your code can decide whether to create an instance or not, without relying on the getInstance of a subclass.


1 A popular saying goes, "If you want a job done right, do it yourself."

2 Here is a link describing a solution based on setAccessible(true)


Need Your Help

Is is possible to use <a title=“<a>”>

javascript href photo title viewer

I am creating a site with some pictures on it of me and my friends. To be able to view those photos quickly I've added the Milkbox javascript to my site (Milkbox Site). You can add photo's like thi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.