However, in certain situations, two or more singletons can mysteriously materialize, disrupting the very guarantees that the singleton is meant to provide. For example, if your singleton Frame is meant as a global user interface for your application and two are created, your application will have two Frame s on the screen -- quite confusing for the user. Further, if two counters are created where one was intended, then clients requesting numbers will not get the desired sequence 1, 2, Additionally, if several instances of a database-connection singleton are created, you might start receiving SQLExceptions complaining about "too many database connections.
In this article, I'll describe those phenomena and how to avoid them. After discussing how to implement the singleton, I'll go over the sometimes surprising causes for the phenomena one by one, showing you how they occur and how you can avoid making those mistakes. I hope that in the process you will learn about classloading, multithreading, distributed systems, Design Patterns, and other interesting topics, as I did.
There are a few ways to implement singletons. Although you can get singleton-like behavior with static fields and methods [for example, java. With singletons implemented as single instances instead of static class members, you can initialize the singleton lazily, creating it only when it is first used.
Likewise, with a singleton implemented as single instance, you leave open the possibility of altering the class to create more instances in the future. With some implementations of the singleton, you allow writers of subclasses to override methods polymorphically, something not possible with static methods.
Most commonly, you implement a singleton in Java by having a single instance of the class as a static field. You can create that instance at class-loading time by assigning a newly created object to the static field in the field declaration, as seen in Listing 1. Alternatively, you can instantiate it lazily, on first demand, as seen in Listing 2.
You keep the constructor private to prevent instantiation by outside callers. Both singleton implementations do not allow convenient subclassing, since getInstance , being static, cannot be overridden polymorphically. Other implementations prove more flexible. While I don't have the space to describe alternative implementations in detail, here are a couple of possibilities:. As I discuss the singleton, keep the above implementations in mind, although many of those phenomena can occur for any implementation of the singleton.
Now that we've looked briefly at implementation, let's turn to the heart of this article: how two singletons can exist simultaneously. When copies of the singleton class run in multiple VMs, an instance is created for each machine. Since intermediate layers can hide the distributed technologies, to tell where an object is really instantiated may be difficult. For a stateless session bean, multiple calls to what appears, to your code, to be one instance could actually be calls to different instances on different VMs.
Even an entity EJB can be saved through a persistence mechanism between calls, so that you have no idea what instance answers your method calls. The primary key that is part of the entity bean spec is needed precisely because referential identity is of no use in identifying the bean. The instance fields of the singleton will not be globally unique. Because several VMs are involved for what appears to be the same object, several singleton objects might be brought into existence.
Singletons that do not hold state but simply control access to resources are also not appropriate for EJBs, since resource management is the role of the EJB container.
However, in other distributed systems, singleton objects that control resources may be used on the understanding that they are not unique in the distributed system, just in the particular VM. October 7, at am. Hemant Chhonkar says:. July 17, at pm. Shailesh says:. September 9, at am. Mayank Mishra says:. August 16, at am.
Suryakant Sapkal says:. December 13, at am. Ethan says:. August 2, at am. Akash says:. July 12, at am. June 29, at pm. Bathula says:. June 30, at pm. July 3, at am. Ambika Solanki says:. August 15, at pm. Wasim says:. June 26, at pm. Pooja says:. June 25, at pm. Sandeep says:. June 3, at am. Patel says:. May 8, at pm. Nitin says:. April 20, at am. Bibhav says:. August 13, at pm. February 27, at am. FStarred says:. February 21, at am.
Marko says:. March 30, at am. Ugur says:. February 20, at am. MIsa says:. February 13, at pm. Neetesh says:. February 4, at am. Dattatray says:. December 27, at am. Gaurav Jain says:. March 7, at am. Manish Kumar Prasad says:. December 24, at pm. May 4, at pm. Barba of the Pale Horse says:. March 27, at pm. GauravD says:. May 8, at am. August 14, at am. Sangchual Cha says:.
October 27, at pm. Rakesh Yadav says:. December 3, at am. Lakhan says:. September 6, at am. Kuldeep Singh says:. August 24, at pm. August 25, at am. Tejas says:. August 5, at am. Disha says:. August 17, at pm.
Asutosh Swain says:. June 19, at am. May 22, at pm. Devendra Singh says:. May 19, at pm. Murali Krishna Varanasi says:. April 28, at am. May 1, at am. Joseph Grigg says:. May 30, at am. April 25, at am. April 11, at pm.
Babanna S Duggani says:. April 6, at am. Neetu says:. April 5, at pm. April 3, at pm. Janaksinh M says:. March 26, at am.
Anupam Tamrakar says:. March 23, at am. March 6, at am. Balaji says:. February 23, at am. Kanij Sheikh says:. February 20, at pm. Prashant Bagade says:. February 18, at am. Anupkumar Mahamalla says:. February 14, at am. February 17, at am. Eddy says:. January 8, at am. January 12, at am. December 24, at am. AUR Dogar says:. January 6, at pm. December 30, at am. December 14, at am.
November 20, at pm. Keval Solanki says:. October 18, at pm. October 4, at pm. Anil kumar says:. September 20, at pm.
Charlie says:. September 8, at pm. September 28, at am. Vishwas Tyagi says:. August 4, at am. Brijesh Baser says:. August 26, at am.
January 7, at am. Sujeet says:. July 25, at am. Rakesh kumar Swain says:. July 15, at am. Krishna Chowdary Garapati says:. December 27, at pm. June 28, at am.
Sameer Patel says:. June 17, at am. Lalit says:. June 8, at pm. Rizwan says:. June 1, at am. Sindhura says:. May 25, at am. Rajeev says:. May 24, at am. April 18, at pm. Dndnnz says:. April 19, at am. Surya says:. March 24, at am. Nagarjuna Aluru says:. March 16, at pm. Harish Kumar Tharala says:. November 20, at am. November 17, at pm. Anurag says:. October 19, at am. Raja says:. October 13, at am. Amsidh Lokhande says:. September 17, at am. Mohit Garg says:. Vasudha Singal says:.
September 16, at am. Himansu says:. September 11, at am. Prasann Mishra says:. August 28, at am. Ravi says:. August 10, at am. Arvind Ajimal says:. September 23, at am. John says:. Bharat says:. July 29, at pm. Rasheed says:. July 27, at am. Manish Kumar says:. October 28, at am. Gayatri says:. July 25, at pm. Suresh reddy says:. July 21, at am. Second, sometimes, not only creating more than one instance is costly, it can take you to an inconsistent state. The answer is, yes, you can make it a global variable.
But, what if that object creation requires heavy resource usage? Global variables might be created just when your application starts. These small things are easily noticeable in the mobile world.
To prevent this, we make use of lazy-loading, which means that we need to create the object only when it is needed. That means, no class should be able to do new Singleton at all.
How do we prevent this? This one is simple. Just make the constructor of the singleton class private. Private constructors can be called from the same class. This is true for private methods and fields, too. Below is a simple example in Kotlin of a class with private constructors. A singleton class should be accessible to the class. Even though we have restricted instantiation of the singleton class, we can still have access to that kind of object. This is by making a static method, which returns the singleton object.
In the following example, we have made a static method that checks to see if the instance is null. Most of the people who implement the singleton pattern get the above two concepts right.
0コメント