Java 单例类如何使用接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1497003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:28:29  来源:igfitidea点击:

How can a singleton class use an interface?

javainterfacesingletonclass-design

提问by AJ.

I read at many places that singletons can use interfaces. Some how I am unable to comprehend this.

我在很多地方都读到过单身人士可以使用接口。有些我无法理解这一点。

采纳答案by nkr1pt

Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that responsibility, but that is another discussion.

每个类都可以实现一个接口,而 Singleton 只是一个“普通”类,它确保在任何时间点除了它可能实现的其他业务逻辑之外,只存在一个它的实例。这也意味着单例至少有 2 个职责,这不是好的 OO 设计,因为类应该只有 1 个职责并确保它们擅长该职责,但这是另一个讨论。

回答by Steve314

A singleton has an instance - it just never has more than one instance. You probably use a couple of static members for reference-fetching and to ensure that it never gets multiple instances, but for the most part the class is the same as any other class.

单身人士有一个实例——它永远不会有多个实例。您可能使用几个静态成员来获取引用并确保它永远不会获得多个实例,但在大多数情况下,该类与任何其他类相同。

回答by Nick Holt

Something like:

就像是:

public interface MyInterface 
{
}

And

public class MySingleton implements MyInterface
{
  private static MyInterface instance = new MySingleton();

  private MySingleton() 
  {
  } 

  public static MyInterface getInstance()
  {
    return instance;
  }
}

回答by Zaki

Basically, a singleton class is a class which can be instantiated one and only once. The singleton class pattern is implemented by using a static method to get the instance of the singleton class and restricting access to its constructor(s).

基本上,单例类是一个只能实例化一次的类。单例类模式是通过使用静态方法获取单例类的实例并限制对其构造函数的访问来实现的。

As with the usage of an interface, it would be similar to the way any other class would implement an interface.

与接口的使用一样,它类似于任何其他类实现接口的方式。

And also it shouldnt allow cloning of that object.

而且它也不应该允许克隆该对象。

回答by Chaithanya Kanumolu

I think I understood your problem. You want to define factory method in interface (static method to getInstance()). But since factory method can't be defined in interface, that logic will not work.

我想我明白你的问题了。您想在接口中定义工厂方法(getInstance() 的静态方法)。但是由于不能在接口中定义工厂方法,该逻辑将不起作用。

One option is to have a factory class which holds that static method. So there will be three classes first class to hold static method second is the interface third is the concrete class

一种选择是拥有一个包含该静态方法的工厂类。所以会有三个类,第一个类是用来存放静态方法的,第二个是接口,第三个是具体的类

But we can not make the concrete constructor private.

但是我们不能将具体的构造函数设为私有。

But if your infrastructure has two packages one for public and the other for private

但是,如果您的基础设施有两个包,一个用于公共,另一个用于私有

define interface in public, make concrete class package level (with out any access modifier) and Factory class and static method be public.

公开定义接口,使具体类包级别(没有任何访问修饰符)和工厂类和静态方法公开。

I hope this could help you.

我希望这可以帮助你。