1

I learned about SINGLETON to be implemented through enums, so that, it retains the singleton nature against reflection also.

Can there be a practical industry/realTime scenario, where such thing could happen ?

My understanding is NO.

When all the code is with me, why will I have such a thing happen to any singleton class at my code.

Rather, such code practice, would be visible to those having access to code repository, so it wont skip the eyes !

Asking to understand, if I am missing some perspective, through which this could actually happen !

Aditya Rewari
  • 2,343
  • 2
  • 23
  • 36
  • 1
    Please share more details and examples to explain your problem. – Suman Aug 12 '20 at 04:41
  • Similar question: https://stackoverflow.com/questions/44171031/why-java-singleton-needs-to-prevent-the-reflection-attack – jaco0646 Aug 12 '20 at 22:51
  • 1
    It’s not like enum types were entirely immune against reflective object creation. In fact, it’s actually easier than most people think. But more than often, people use the Singleton pattern because they want a shared instance accessible through a global variable whereas creating a new instance would counter intention but not cause that much harm. In fact, some people doubt that there are actual examples of real use cases for the Singleton pattern. – Holger Aug 13 '20 at 11:46
  • @Holger, Java's `class` objects are an example of the Singleton design pattern (referred to in the GoF book as _metaclasses_). – jaco0646 Aug 14 '20 at 00:20
  • 1
    @jaco0646 in a typical Java environment, there thousands of `Class` instances. That’s far away from being a singleton. – Holger Aug 14 '20 at 07:03
  • @Holger, I'm just repeating the book. All of the GoF patterns were drawn from actual examples. They didn't invent any patterns on their own, so every chapter ends with "Known Uses". In the Singleton chapter, they mention one example from Smalltalk, one from C++, and, "_A more subtle example is the relationship between classes and their metaclasses. A metaclass is the class of a class, and each metaclass has one instance._" That sounds like Java's `class` objects. – jaco0646 Aug 14 '20 at 12:58
  • 1
    @jaco0646 I can only repeat myself “*in fact, some people doubt that there are actual examples of real use cases for the Singleton pattern*”. This includes the examples given by the GoF. A class like `java.lang.Class` that has thousands of instances is not a Singleton, regardless of what the GoF wrote in their book. – Holger Aug 14 '20 at 13:19
  • @Holger, who are these people and where is their definition of a Singleton? Citation needed. – jaco0646 Aug 14 '20 at 14:02
  • 1
    @jaco0646 who said that there was another definition? The GoF’s definition is sufficient, a class of which *only one* instance exists. If you can’t see that a class which has thousands of instances contradicts that definition, any discussion is pointless. Can you name a real life example of a class that would fail if more than one instance of it exists? – Holger Aug 17 '20 at 08:29
  • @Holger, after reading an article by [Brian Goetz](https://cr.openjdk.java.net/~briangoetz/erasure.html#homogeneous-vs-heterogeneous-translations) I think the GoF quote was probably referring to metaclasses similar to C++ and not Java (i.e. heterogeneous rather than homogeneous generic translation according to the article). – jaco0646 Oct 20 '20 at 19:50

1 Answers1

3

If you are asking if it is possible to break a singleton implemented some other way, then yes it is possible:

  • You can use reflection to gain access to a private field
  • You can use reflection to update a final field.

Is it practical? Well it depends on what you mean by practical.

Could it happen in practice? Yes.

We cannot enumerate the reasons that someone might need (or choose) to do such a thing. But suppose that you had some pressing reason to change the value of a singleton, and you had lost all of your source code. (Yes. It happens!) Or supposed that you never had the source code in the first place.

When all the code is with me, why will I have such a thing happen to any singleton class at my code.

If you have the source code, and the freedom to change and recompile the source code, then it would be a bad idea to break a singleton using reflection. If it is necessary (for pragmatic reasons) to break the singleton's invariants, then it is better to modify the API so that is clear what you are doing and why.

Rather, such code practice, would be visible to those having access to code repository, so it wont skip the eyes !

Just because someone has access doesn't mean that they will bother to look. And just because they look doesn't mean they will see. (There are ways to hide code that does dubious things within an application code base.)


Anyhow, the point is that this kind of thing is possible for some versions of the singleton pattern (and not others) and anything that is possible could happen if the circumstances allowed or required it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216