I am using SOAP which initialization takes some time so I don't want to this with every request. As far as I know singleton is not proper pattern for this situation. I use static factory method for this which is mentioned in What are static factory methods in Java? Is it good practice?
public class ServiceConnection {
public static final int MAX_CONNS = 5;
private static int totalConnections;
public static Set<Service> serviceIterator = new HashSet<>();
private ServiceConnection(){
totalConnections++;
}
public static Service getService(){
if(totalConnections < MAX_CONNS){
Service service = new Service();
serviceIterator.add(service);
return service;
}else{
return serviceIterator.iterator().next();
}
}
}
Aucun commentaire:
Enregistrer un commentaire