In this article we will see one of the common creational patterns, the Singleton . This pattern ensures that a class has only one instance and provides a global point of access to it.
This pattern is often used when one class controls access to a single physical resource or when data must be available to all application objects.
The Singleton pattern is implemented by a class a private constructor. There will be a method that will instantiate the object by calling the constructor only if there is still no.
can cause problems for programs with multiple threads, if two threads try to create the instance at the same time and it does not exist yet. That only one of them can create the object you can use mutual exclusion (synchronized ) in the method of creating a class that implements the pattern .
should say also that if multiple class loaders for the application may be an instance of the class in each of them, but the instance is unique in that magazine.
A successful deployment of Singleton pattern in Java could be the next Bill proposed by the researcher Pough:
public class Singleton {/ / Private constructor Prevents instantiation from classes
Other private Singleton () {}
/ ** * SingletonHolder
is loaded on the first execution of
* Singleton.getInstance () or the first access to
* SingletonHolder.INSTANCE, NOT before.
* / private static class
SingletonHolder {private static final Singleton INSTANCE = new Singleton ();}
public static
Singleton getInstance () {return
SingletonHolder.INSTANCE;
}}
Although widely used, there is a stream against the use of this pattern due to the problems it introduces. On this page Scout Densmore explains the problems attributed to Brian Button Singletons. Then do an adaptation of what is said there:
- The Singletons are usually used to provide a global point of access for service. Thus no need to pass a reference to this service, which is like using a global variable. In the end what happens is that the design units remain hidden within the code and are not visible when examining the interfaces. We need to inspect the code to understand what objects are using the classes.
- The Singletons will limit the creation of objects. This is true, but now they are mixing two different responsibilities in the same class. A class should not worry about whether or not a Singleton, but you should only worry about their business responsibilities. To limit the creation of classes should be set up factories or other objects that restrict the creation. Thus, each object would maintain a better-defined responsibilities.
- The Singletons promote tight coupling between classes . A loose coupling between classes can substitute alternative implementations of collaborating classes in testing. A better alternative is to modify the design to pass references to objects in the classes and methods so as to reduce the coupling.
- The Singletons maintain the state until the completion of the program. This persistent state is detrimental to the unit tests, since each test must be independent of the others. If this is not met, could lead to situations where the tests fail where they should not because they are hiding mistakes. You can avoid passing object references to lectures and methods.
is why it looks like a design pattern to avoid whenever possible.
References:
Singleton Pattern in Wikipedia
Singleton Pattern - Design Patterns
Singletons in Java, the standard single instance David Barral
Singleton Pattern in c2.com Why Singletons Are
Evil by Scott Densmore
Other design patterns