How can a singleton class use an interface?
Asked Answered
F

5

12

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

Florri answered 30/9, 2009 at 9:23 Comment(0)
S
23

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.

Syrinx answered 30/9, 2009 at 9:26 Comment(2)
Why a Singleton is not good OO design? In fact is one of the most common OO design patternsOkeefe
@ChristianVielma: (Yes, late) The issue is it creates global state, just like global variables, and suffers from similar issues. For example, it's difficult to test classes that internally request singletons without outside knowledge. Your production DB is full of garbage/test data? Check for singletons. Hunting down a bug? If you have singletons, and therefore global state, you can't just focus on a limited scope; you must keep the code-base in mind. Thank a singleton. Note that this is about the singleton pattern, not singleton objects. Common use does not mean it must be "good".Ineluctable
L
10

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;
  }
}
Listed answered 30/9, 2009 at 9:28 Comment(4)
FYI, the best way in Java to implement a Singleton is by using a single-element enum. It is more concise than the public field approach and it provides the serialization mechanism for free and it also provides security against reflection attacks. This method has yet to be widely adopted but it might be interesting to know. For more information see the item about it in "Effective Java" by Joshua Bloch.Syrinx
@Andrew: thanks, I was bouncing between SO and the day job and missed that by accident :-)Listed
@nkr1pt: I generally try to avoid singletons but using an enum is neat idea, thanks for the pointer.Listed
@Syrinx I had never thought of that technique. That really is powerful, yet shockingly simple. It also massively simplifies the referencing and makes it very intuitiveMythopoeia
E
7

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.

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.

Electropositive answered 8/4, 2013 at 11:38 Comment(0)
S
0

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.

Shot answered 30/9, 2009 at 9:28 Comment(0)
H
0

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.

Harshman answered 30/9, 2009 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.