4

I came across a question on Design Patterns.

It asked for the apt design pattern to use in a scenario where exactly two instances of a class are ever created. I was not sure of the answer but the description later said that the Singleton can be used for such a scenario. It said that the Singleton can ensure that either only one instance of a class or a fixed number (other than one) are created.

I have always read the Singleton pattern as a pattern where only one object of a class can be created and thus the explanation surprised me. I am not sure I agree with it.

Any thoughts?

Aditya

Aditya K
  • 448
  • 3
  • 11
  • What I think you want is called a _pool_ where there are a fixed number of objects and you can get one and then later release it back into the pool. This is commonly done with threads in a thread pool. – Dan D. Feb 23 '14 at 09:13
  • 1
    For sure you can implement something like that, but I would not call it "Singleton Pattern" any longer. – Henry Feb 23 '14 at 09:14
  • Not actually related, but singleton as a design pattern has long been considered an anti-practice. If you want to use real singletons, you should resort to dependency injection with `@Singleton` annotations or the like – Stefano Sanfilippo Feb 23 '14 at 09:31

3 Answers3

2

Here's an example demonstrating a Multiton class which holds exactly ever n objects:

public class Multiton
{
  private static Multiton[] instances;

  private Multiton() {}

  public static void initializeWithNumberOfInstances(int number)
  {
    instances = new Multiton[number];
  }

  public static Multiton getInstanceAtIndex(int index) throws Exception
  {
    if (instances == null)
      throw new Exception("Initialize number of instances first");

    if (instances[index] == null)
    {
      instances[index] = new Multiton();
    }

    return instances[index];
  }
}
Aman Agnihotri
  • 2,805
  • 1
  • 14
  • 21
0

Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:

1.Ensure that only one instance of a class is created
2.Provide a global point of access to the object
3.Allow multiple instances in the future without affecting a singleton class's clients

as from above it stated that you can only create only one instance of class and use it anywhere.If we think so we can actually make the two instances also.

class Singleton
{
     private static Singleton instance1 = null;
     private static Singleton instance2 = null;
     private Singleton{}
     public static Singleton getInstance()
     {

         if(instance1 == null)
         {
               instance1 = new Singleton();
               return instance1;
         }
         else if(instance2 == null)
         {
               instance2 = new Singleton();
               return instance2;
         }
         return instance1;
     }
}

if you want create more than 2 then you can create in getInsatance but better to create an array of class.

Devavrata
  • 1,655
  • 13
  • 26
0

The multiton has common applications in managing finite pools (e.g. threads, connections) and also controlling / throttling the number of consumers in producer-consumer patterns, e.g. here. The term "Registry of singletons" can also be used, although this possibly refers to a common implementation of keying the instances in a Dictionary or HashMap, rather than the design concept.

As per @Stefano's comment, the original GOF singleton implementation of using statics to manage the instances should be replaced with an external management mechanism such as an IoC container controlling the creation and lifespan of instances.

Community
  • 1
  • 1
StuartLC
  • 96,413
  • 17
  • 181
  • 256