How can a singleton class use an interface?

后端 未结 5 1887
盖世英雄少女心
盖世英雄少女心 2021-02-04 11:09

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

5条回答
  •  囚心锁ツ
    2021-02-04 11:46

    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;
      }
    }
    

提交回复
热议问题