ThiagoWorldCoder:
No singleton, eu sei que eu garanto uma única instancia de uma classe durante toda a vida da minha aplicação.
Entretanto, eu estou precisando passar alguns valores para esse singleton, de modo que esses valores também não morram e possam ser trocados à qualquer hora!
valeu!
First, we create an interface for objects that create instances of the Singleton class. This is essentially a combination of the Abstract Factory, Factory Method and Functor patterns in the GoF book.
/**
* An interface defining objects that can create Singleton
* instances.
*/
public interface SingletonFactoryFunctor {
/**
* @return An instance of the Singleton.
*/
public Singleton makeInstance();
}
Second, we create static wrapper for the Singleton class. Note that by separating the wrapper from the actual implementation, it is possible to both A) provide a well known access point for the Singleton instance and B) allow greater flexiblity as to which subclass to use to create the instance. Note that the wrapper could actually wrap a Singleton interface, rather than a concrete class.
/**
* A static container for a single instance of the Singleton
*/
final public class SingletonWrapper {
/**
* A reference to a possibly alternate factory.
*/
static private SingletonFactoryFunctor _factory = null;
/**
* A reference to the current instance.
*/
static private Singleton _instance = null;
/**
* This is the default factory method.
* It is called to create a new Singleton when
* a new instance is needed and _factory is null.
*/
static private Singleton makeInstance() {
return new Singleton();
}
/**
* This is the accessor for the Singleton.
*/
static public synchronized Singleton instance() {
if(null == _instance) {
_instance = (null == _factory) ? makeInstance() : _factory.makeInstance();
}
return _instance;
}
/**
* Sets the factory method used to create new instances.
* You can set the factory method to null to use the default method.
* @param factory The Singleton factory
*/
static public synchronized void setFactory(SingletonFactoryFunctor factory) {
_factory = factory;
}
/**
* Sets the current Singleton instance.
* You can set this to null to force a new instance to be created the
* next time instance() is called.
* @param instance The Singleton instance to use.
*/
static public synchronized void setInstance(Singleton instance) {
_instance = instance;
}
}
Clients that use this SingletonWrapper need only use something like SingletonWrapper.instance().method().
Note that since SingletonWrapper is final, the instance method can essentially be inlined. Hence the only real over-head here is the check if _instance is null. (Even this could be removed, if we refactor the class to ensure that _instance is never null. For example:
* create a new instance when the class is loaded via a static initializer
* create a new instance when a new factory is assigned via setFactory
* make sure setInstance is never used to change _instance to null, or create a new instance if it is)
This approach allows us to easily alter the specific Singleton instance published--either dynamically or at compile-time--without altering the code that uses the Singleton. Moreover, the Singleton itself need not be aware of the Singleton functionality. Hence this SingletonWrapper can be created for pre-existing classes such as those provided with an API or framework.
[size=18]FONTE:[/size]
:arrow: http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html