Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a factory that returns singletons

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?

like image 435
12rad Avatar asked Dec 09 '13 12:12

12rad


People also ask

What is a singleton factory method?

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.

Can you give some examples of where singletons might be a good idea?

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.

Can abstract factory use singleton pattern?

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.


2 Answers

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.

like image 130
Narendra Pathai Avatar answered Oct 11 '22 03:10

Narendra Pathai


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

like image 41
Raffaele Rossi Avatar answered Oct 11 '22 03:10

Raffaele Rossi