Reinforce the singleton pattern

  • 16
  • Locked
Let's delve into the singleton pattern by considering the sun and planets of the solar system. First, do an Internet search and find some example of "lazy initialization" using the singleton pattern. Create three singleton classes "in its likeness and image": Sun, Moon and Earth. Then implement the Planet interface in them. And do a couple other operations.
You can't complete this task, because you're not signed in.
Comments (24)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Justin Smith
Level 39 , Greenfield, USA, United States
20 August 2021, 15:24
Two things that stumped me for a while on this one. 1. When the conditions say "The Sun class must have a private static Sun field instance" the use of "instance" in that sentence does not mean "it's an instance of the Sun class". It means it literally wants you to name the field "instance" and you will fail validation if you don't. For clarity purposes, sentences like this should put "instance" in quotation marks, or change the sentence to say "The Sun class must have a private static Sun field named instance". The word "instance" has meaning in programming that makes the sentence ambiguous and easily misinterpreted. 2. The instructions say to google how to create a Singleton class, but if you follow the instructions you find, you will fail validation. This is because the traditional way that you will find in Google searches is to initialize the static object of the class in the declaration (i.e. "private static Sun instance = new Sun();" This task, however, doesn't want the objects created until they are needed by specific call of the readKeyFromConsoleAndInitPlanet method.
Khongpak Phupkdee
Level 15 , Chiangrai, Thailand
11 October 2021, 07:07
i agree with you
Jonaskinny Java Developer at Sandmedia
8 March 2022, 23:16
Lazy loading is the difference between declaring + initializing the instance, and what they want here. In practice I've never seen a singleton that was not lazy.
Level 15 , Old Town, United States
26 June 2021, 14:16
another introduction to new materials by working on a task! love it . . . (not really:)
Level 24 , Wittenberg, Germany
4 June 2021, 20:07
task1522 (Reinforce the singleton pattern) caveat: as usual, one should pay much more attention to the formal requirements than to the free description that comes before. The requirements prescribe the name of the singleton instance field and that of the singleton instance getter, as well as exact accessibility modifiers for various things. Requirements 5, 10 and 15 ('The field instance must be initialized after the first call to the getInstance method, but not before.') is nonsense and unfulfillable if taken literally, i.e. if decoded as an English sentence instead of pseudo-formal programmer speak. Translation: 'Before the first call to the getInstance() method, the field instance must be in an uninitialised state; after the first call it must be in an initialised state.' Needless to say, naming the interface CelestialBody instead of Planet would have avoided the problem of the interface name being flat out wrong for most of the classes it is used with. Choosing good names is a difficult but necessary skill to develop; giving poor examples doesn't exactly help aspiring padawans.
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
15 April 2021, 03:30
well, it only works if the variable is named what they said, but the name they said did not seem like a name to me, but a description, or designation. Changing to the designated name fixed it all for me
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
14 April 2021, 23:56
Does it bother any others that we are pretending the moon is a planet? :)
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
14 April 2021, 23:57
And the Sun...too :)
Level 27 , Heiloo, Israel
30 March 2021, 20:25
1- We have various ways of creating singletons in Java. Now, first of all, what is Singleton and why is it required? The singleton design pattern is used to restrict the instantiation of a class and ensures that only one instance of the class exists in the JVM. In other words, a singleton class is a class that can have only one object (an instance of the class) at a time per JVM instance. There are various ways to design/code a singleton class. Class-level Member (Eager Initialization Method): Make constructor private. Make a private constant static instance (class-member) of this Singleton class. Write a static/factory method that returns the object of the singleton class that we have created as a class-member instance. We can also mark a static member as public to access constant static instance directly. But, I like to access class/instance members via methods only. So, the singleton class is different from a normal Java class in terms of instantiation. For a normal class, we use a constructor, whereas for singleton class we use the getInstance()method. public class SingletonClass { private static final SingletonClass SINGLE_INSTANCE = new SingletonClass(); private SingletonClass() {} public static SingletonClass getInstance() { return SINGLE_INSTANCE; } } 2- Class-level Member (Lazy Initialization Method): Make constructor as private. Make a private static instance (class-member) of this singleton class. But, DO NOT instantiate it. Write a static/factory method that checks the static instance member for null and creates the instance. At last, it returns an object of the singleton class. public class SingletonClass { private static SingletonClass SINGLE_INSTANCE = null; private SingletonClass() {} public static SingletonClass getInstance() { if (SINGLE_INSTANCE == null) { synchronized(SingletonClass.class) { SINGLE_INSTANCE = new SingletonClass(); } } return SINGLE_INSTANCE; } } For more
Level 15 , United Kingdom
14 March 2021, 20:33
synchronized method is frowned upon by most answers I found on Google as it can slow down the system on occasion. Not sure what "volatile" is either as I dind't see it anywhere in my Google search. I used the simpler implementation of Lazy Initilization I found in for single-threaded applications as I don't think it makes a difference. Reason being I don't see the benefit of lazy initialization when every implementation pattern has serious drawbacks. enum is the way to go if you want to do a singleton pattern based on most articles.
Jonaskinny Java Developer at Sandmedia
8 March 2022, 23:18
in this case it does not make a difference, but the point is to teach singleton while teaching lazy and synchronized.
Level 18 , Sofia, Bulgaria
17 February 2021, 10:31
volatile?? synchronized?? ... Can someone send me PM to explain this... I got it working without even knowing about these...
Jonaskinny Java Developer at Sandmedia
8 March 2022, 23:20
You can do it without either, but then it would not be a thread-safe implementation. It's confusing here since the use case does not require what they are wanting us to implement so we understand the concepts, since they often go together but again not in this case.
Oliver Heintz
Level 18 , Mustang, United States
20 January 2021, 02:55
I had my program looking for the strings: "Sun", "Moon", and "Earth", but the validation would only pass if the strings were "sun", "moon", and "earth". So stupid. I see where it says "sun" on input data now, but I typically only open the tasks on IntelliJ where sample input is not displayed. And my idiot self didn't even look that hard at the Planet file. I guess I deserved to fail.
Jonaskinny Java Developer at Sandmedia
8 March 2022, 23:21
anytime you see static final strings being used that's your trigger to use xxx.equals(Planet.MOON) etc. That's true here and in real.
Ian De Bie Full Stack Developer
14 October 2020, 18:09
this was my favorite task so far. if anyone needs help with this one then let me know...