I have common code (multiple class that I call controllers) that needs to be shared by multiple packages in the project. I was thinking of creating a factory, that returns these controllers. So, the factory would have a hashmap, that can return the controller asked for or create a new one if not created. The controllers have common code and since I don't need to create multiple instances of these controller, I think they should be singletons.
Does this seem like a good approach?
Singleton – Ensures that at most only one instance of an object exists throughout application. Factory Method – Creates objects of several related classes without specifying the exact object to be created.
It is used where only a single instance of a class is required to control the action throughout the execution. A singleton class shouldn't have multiple instances in any case and at any cost. Singleton classes are used for logging, driver objects, caching and thread pool, database connections.
Abstract Factory, Builder, and Prototype can use Singleton in their implementation. Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system.
I think what you need is a Multiton pattern.
The multiton pattern is a design pattern similar to the singleton, which allows only one instance of a class to be created. The multiton pattern expands on the singleton concept to manage a map of named instances as key-value pairs. Rather than have a single instance per application (e.g. the java.lang.Runtime object in the Java programming language) the multiton pattern instead ensures a single instance per key.
public class FooMultiton {
private static final Map<Object, FooMultiton> instances = new HashMap<Object, FooMultiton>();
private FooMultiton() {
// no explicit implementation
}
public static synchronized FooMultiton getInstance(Object key) {
// Our "per key" singleton
FooMultiton instance = instances.get(key);
if (instance == null) {
// Lazily create instance
instance = new FooMultiton();
// Add it to map
instances.put(key, instance);
}
return instance;
}
// other fields and methods ...
}
The controllers have common code and since I don't need to create multiple instances of these controller, I think they should be singletons.
You need single instance does not necessarily mean that you need a Singleton Pattern. You can have a single instance and pass that on subsequent calls to that. Don't necessarily enforce singletonness using private
constructor.
Also read Evil Singletons for more on down points of Singletons. After reading that if you still feel you need Singleton then go with it.
Yes it does. The Singleton design pattern suffer for several problems, one of which is that Singletons cannot be extended.
However, if you retrieve these controllers via a ControllerFactory.createControllerX() instead of ControllerX.getInstace(), you will be able to extended ControllerX in future and all its client will not be aware of using an extended version. Which is a really good thing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With