This example shows creating an Nton. Means the application is provided
with N-instances of a particular class. Each call to getInstance method is
served with one of the available instances in round robin fashion.
The number of instances required by the application is configurable.
Behaviour & Advantages
The number of instances required by the application should be configurable.
The class should have a private constructor to avoid creation of external instances.
It should have static synchronized method which returns one of the available instances
in round robin fashion (Thread synchronized has to be taken care to avoid creation of duplicate instances)
In case of service based Nton's, this provides a way of load balancing by delegating the
responsibility of handling the request to one of the available and identical services.
/**
* Configure the number of instances required in the application.
*/ private static final int NUM_OF_INSTANCES = 5; private static final List<Nton> instanceList = new ArrayList<Nton>(); private static int instanceRequestCount = 0; private int instanceNum;
/**
* Make sure to have private default constructor.
* This avoids direct instantiation of class using
* new keyword/Class.newInstance() method
*/ private Nton() {
}