How do I restrict object creation not more than 3 in Java class?
How do I restrict object creation n开发者_如何学JAVAot more than 3 in Java class?
Can you give me an idea of how I can to do it?
We can restrict the creation of Object for a particular class by little modification in Singleton design pattern as below:
public class LimitClass {
private static LimitClass limInstance;
public static int objCount = 0;
private LimitClass(){
objCount++;
}
public static synchronized LimitClass getLimInstance(){
if(objCount < 3 ){
limInstance = new LimitClass();
}
return limInstance;
}
}
public class LimitObjectCreationTest {
public static void main(String[] args) {
LimitClass obj1 = LimitClass.getLimInstance();
LimitClass obj2 = LimitClass.getLimInstance();
LimitClass obj3 = LimitClass.getLimInstance();
LimitClass obj4 = LimitClass.getLimInstance();
LimitClass obj5 = LimitClass.getLimInstance();
LimitClass obj6 = LimitClass.getLimInstance();
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
System.out.println(obj6);
}
}
Output of above code is like:
com.pack2.LimitClass@19821f
com.pack2.LimitClass@addbf1
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816
After creation of three different object it is repeating the same 3rd object again and again. It is not creating the different object.
Instanciate your class via a factory (see design patterns) and limit it to 3 instances using a simple counter.
You need Modified Singleton. See this - same OOPS (Design Patterns)
Factory pattern is the way to go. Or a static counter can be used. Need to careful about thread safety.
You can also have a look at ObjectPool of Apache Commons. The source code is freely available...
why not assign limInstance=null ; in else case in getLimInstance() ? in that case it will not provide obj3 duplicates ?
public static synchronized InstanceClass getLimInstance()
{
if(objCount < 3 ){
limInstance = new InstanceClass();
}
else
{
limInstance=null;
}
return limInstance;
}
public class LimitObjectCreationTest {
public static void main(String[] args) {
InstanceClass obj1 = InstanceClass.getLimInstance();
InstanceClass obj2 = InstanceClass.getLimInstance();
InstanceClass obj3 = InstanceClass.getLimInstance();
InstanceClass obj4 = InstanceClass.getLimInstance();
InstanceClass obj5 = InstanceClass.getLimInstance();
InstanceClass obj6 = InstanceClass.getLimInstance();
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
System.out.println(obj6);
System.out.println(obj3);
}
}
Result was this :
come.classinstance.InstanceClass@4a5ab2
come.classinstance.InstanceClass@1888759
come.classinstance.InstanceClass@6e1408
null
null
null
come.classinstance.InstanceClass@6e1408
You need to modify singleton and create three objects.ans write three method in every method return object like as return different three objects and use when you needed. it main use no need to declare variable or object as static, because always return same object.
if you want to limit the class instances to N, keep creating a new instance and add it to a collection till the size of the collection is less than N. Else return any of the N instances randomly.
public class LimitClass {
private static List<LimitClass> ObjectPool = new ArrayList<LimitClass>();
public static int objCount = 0;
private static Random randomizer = new Random();
private LimitClass(){
objCount++;
}
public static synchronized LimitClass getLimInstance(){
if(objCount < 3 ){
LimitClass lc = new LimitClass();
ObjectPool.add(lc);
return lc;
}
return ObjectPool.get(randomizer.nextInt()%3);
}
}
public class LimitObjectCreationTest {
public static void main(String[] args) {
LimitClass obj1 = LimitClass.getLimInstance();
LimitClass obj2 = LimitClass.getLimInstance();
LimitClass obj3 = LimitClass.getLimInstance();
LimitClass obj4 = LimitClass.getLimInstance();
LimitClass obj5 = LimitClass.getLimInstance();
LimitClass obj6 = LimitClass.getLimInstance();
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
System.out.println(obj6);
System.out.println(obj3);
}
}
LimitClass@1788a393
LimitClass@460c2da6
LimitClass@4283a56d
LimitClass@460c2da6
LimitClass@1788a393
LimitClass@4283a56d
LimitClass@4283a56d
精彩评论