2

I am confused as to how the multiton implementation of the singleton pattern works. I am aware that the definition of a singleton is as follows:

Ensure a class allows only one object to be created, providing a single point of access to it.

However, when using the enum version of the singleton pattern, would the multiton not allow for more than one object of the class to be created?

For example:

Public enum myFactory{

INSTANCE1, INSTANCE2;

//methods...

}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • there will always be just 1 instance of INSTANCE1 which will be shared by every class that uses it – Stultuske May 22 '14 at 11:26
  • 2
    *would the multiton not allow for more than one object of the class to be created* it would, that is the point ;-) – Tim May 22 '14 at 11:27
  • A singleton doesn't (by itself) prevent other instances of the class from being created, it simply assures that one and only one instance will be created using the prescribed singleton access technique. Your (gag) "multiton" has multiple prescribed access techniques. – Hot Licks May 22 '14 at 11:30
  • From the Wikipedia article you linked: `Rather than having a single instance per application, the multiton pattern instead ensures a single instance per key.` This is exactly what you achieve by using an enum with multiple values. If you like, you could use the enum name as the key and have a static method to retrieve the instances too. – Dan Temple May 22 '14 at 11:40

2 Answers2

3

Multiton Design Pattern

The Multiton design pattern is an extension of the singleton pattern. It ensures that a limited number of instances of a class can exist by specifying a key for each instance and allowing only a single object to be created for each of those keys.

So Enum is best example

http://www.ritambhara.in/multiton-design-pattern/

Community
  • 1
  • 1
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
1

Another way using Singleton pattern is:

To limit for 4

package org.dixit.amit.immutable;

import java.util.Random;

public class ThreadSafeSingleton {


private ThreadSafeSingleton(){

}

private static ThreadSafeSingleton threadSafeSingleton[] = new ThreadSafeSingleton[3];
static int i = -1;

public static ThreadSafeSingleton getInstance(){
    i++;
    System.out.println("i is   ---> "+i);
    if(i<3 && threadSafeSingleton[i]==null){
        synchronized (ThreadSafeSingleton.class) {
            if(threadSafeSingleton[i]==null){
                threadSafeSingleton[i] = new ThreadSafeSingleton();
                return threadSafeSingleton[i];
            }
        }

    }

     int j = randInt(0, 4);

    return threadSafeSingleton[j];

}

private static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}


}
xav
  • 5,452
  • 7
  • 48
  • 57