简体   繁体   English

Java:基础类中的单例

[英]Java: singleton from base class

Lets assume we've got base class: 假设我们有基类:

public class Base {
      public Base(int i) {
      /* ... */
      }
      /* ... */
}

We want to create singleton based on this class. 我们要基于此类创建单例。 The problem is that class have public constructor, so extending it will result in singleton having public constructor, too: 问题在于类具有公共构造函数,因此扩展它会导致单例也具有公共构造函数:

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  public SingletonBase(int i) {  // Want to avoid this!
     super(i) 
  }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

Singleton, of course, cannot have public constructors, so we want to avoid this. 当然,Singleton不能具有公共构造函数,因此我们要避免这种情况。 So, what is most elegant way/pattern to have proper Singleton with functionalities from class Base? 那么,使用Base类中的功能使适当的Singleton成为最优雅的方式/模式是什么?

Edit: Modifying Base class is prohibited, since it is from an external library. 编辑:禁止修改基类,因为它来自外部库。

Constructors are not inherited. 构造函数不是继承的。 So just 所以就
delete this part and you'll get what you need. 删除此部分,您将获得所需的东西。

public SingletonBase(int i) {  // Want to avoid this!
    super(i) 
}

Additionally, you may want to make the Base class 此外,您可能想使Base
abstract so that no one can instantiate it by mistake 抽象,因此没有人可以错误地实例化它
by calling the Base constructor directly. 通过直接调用Base构造函数。

public abstract class Base {
    public Base(int i) {
        /* ... */
    }
    /* ... */
}

public class SingletonBase extends Base {
    private static final SingletonBase _instance = new SingletonBase();

    private SingletonBase() {
        super(0);
    }

    public static SingletonBase getInstance() {
        return _instance;
    }
    /* ... */
}

You could make SingletonBase just a normal subclass and delegate the Singleton-part to a different class. 您可以将SingletonBase普通子类,然后将Singleton部分委托给其他类。 I don't know what your specific context is, but you would have Subclass extends Base and somewhere else you would have your SingletonContainer or something like that where you have the method public static Subclass getInstance() . 我不知道您的特定上下文是什么,但是您将让Subclass extends Base并且在其他地方将拥有SingletonContainer或类似的具有public static Subclass getInstance()

That way, you would have your usual inheritance and also the Singleton effect, since the SingletonContainer would be responsible for keeping only a single instance of Subclass. 这样,您将拥有通常的继承以及Singleton效果,因为SingletonContainer仅负责保留Subclass的单​​个实例。

It is not because you inherit from a class that have a public constructor that you have to create the same public constructor, you can do : 这不是因为您从具有公共构造函数的类继承而必须创建相同的公共构造函数,所以可以这样做:

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

Or even : 甚至 :

public class SingletonBase extends Base {
   private static final SingletonBase _instance = new SingletonBase();

   private SingletonBase() {
      super(0);
   }

  private SingletonBase(int i) {  // Want to avoid this!
     super(i) 
  }

  public static void getInstance() {
     return _instance;
  }
  /* ... */       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM