Reputation: 508
There is a standalone Java application. In it there is a factory method which is called once and creates only one object (if it is possible to create it). I have two questions - which pattern is better for this? And secondly- is it correct to store the object that creates the factory in the factory itself in this case?
Upvotes: 4
Views: 1963
Reputation: 3939
The most common pattern for creating a single object in Java is the Singleton Pattern. According to Design Patterns: Elements of Reusable Object-Oriented Software that recorded the pattern, the purpose of the Singleton Pattern is as follows:
Ensure a class only has one instance, and provide a global point of access to it
There are two ways to create a singleton object: Eagerly and lazily. Eager instantiation is simple because it amounts to the creation of an object and returning it through a getter:
public EagerSingleton {
private static final EagerSingleton INSTANCE = new EagerSingleton();
private EagerSingleton() {}
public static EagerSingleton getInstance() {
return INSTANCE;
}
}
If the creation of the object is expensive (and therefore should be delayed as long as possible) or if there is a reasonable chance that the object may never be needed, it may be a good idea to instantiate the singleton lazily. This is a deceptively difficult task because multiple threads may access the lazily instantiated object and the implementation must ensure that only one object is ever created, regardless of the number of concurrent accessed to the object (i.e. if two threads both see that the singleton has not been created, only one object should be created, not two, and used by both threads).
The safe way to implement a lazy singleton is as follows:
public class Singleton {
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
This ensures that the nested static object is not created until getInstance
is called. There are a number of optimizations of this scheme, but unless performance is severely needed, the above should be used. Custom implementations of this pattern are difficult to make safe.
Upvotes: 2
Reputation: 589
It's better to use Singleton design pattern when you want to create an object only once and which can be used throughout the application. If you are using factory design pattern for creating only one object and used throughout the application then you may use Singleton pattern for this.
In order to create factory of factory objects then use Abstract Factory design pattern. I have written 60 Java/J2EE design patterns.
Please refer the link - https://ramesh-java-design-patterns.blogspot.in/
Upvotes: 1
Reputation: 131326
Factory seems to be adapted to your requirement.
Note that if you want to master the number of instances of the objects created, a simple way would be to define the class of the instance to create as the factory class itself by defining a private constructor and a static method to create the object is a way.
Note that this way may create a strong coupling between the implementation and its clients. So you could use a less coupled solution such as dependency injection or implementations using an interface.
Upvotes: 1