AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-design-patterns: How to restrict a class to make only 3 objects?

Hi,
If i want to restrict a class to allow them to create only 3 object how we will achieve this ? can singleton will help in this situation ?

java x 211
design-patterns x 15
Posted On : 2014-12-23 10:49:07.0
profile Adarsh S - anyforum.in Adarsh S
3100
up-rate
5
down-rate

Answers


Yes, singleton design pattern will be helpful for this kind of situation. We just need to do some modification in singleton class. We can restrict our java class for creating specified number of objects of it.

A singleton class is a class which always returns a single object at any time of application life cycle. To create a singleton class, we need to follow the singleton design pattern.

Steps required to implement a singleton class:
--------------------------------------------------------------------------------------
Step 1: Create a class variable which is basically a reference of it?s own class.
Step 2: Make the constructor private.
Step 3: Create a static method which returns a reference of it?s own class.
Step 4: In the method, check the class variable for null. if it is null create an instance of the class and assign to the class variable.

Now following is the sample code to return the specified no of objects of a class.


RestrictedClassForMultipleObjects.java:
-------------------------------------------------------------------------
public class RestrictedClassForMultipleObjects
{
private static RestrictedClassForMultipleObjects limitedNoOfObjectClass;
public static int noOfObject = 0;
private RestrictedClassForMultipleObjects() {
noOfObject++;
}
public static synchronized RestrictedClassForMultipleObjects getLimInstance()
{
if (noOfObject <3)
limitedNoOfObjectClass = new RestrictedClassForMultipleObjects();
return limitedNoOfObjectClass;
}
}



ObjectCreationTest.java:
----------------------------------------------
class ObjectCreationTest
{

public static void main(String args[])
{

RestrictedClassForMultipleObjects obj1 = RestrictedClassForMultipleObjects.getLimInstance();
RestrictedClassForMultipleObjects obj2 = RestrictedClassForMultipleObjects.getLimInstance();
RestrictedClassForMultipleObjects obj3 = RestrictedClassForMultipleObjects.getLimInstance();
RestrictedClassForMultipleObjects obj4 = RestrictedClassForMultipleObjects.getLimInstance();
RestrictedClassForMultipleObjects obj5 = RestrictedClassForMultipleObjects.getLimInstance();

System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
}
}


Compile and run ObjectCreationTest class and you will get 3 objects only and if you demand more it will return the third object each time..

Posted On : 2014-12-23 13:15:40
Satisfied : 4 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250050
Reply This Thread
up-rate
7
down-rate

Thanks for this but i have one more question, what is preferred for Singleton class, to use synchronized block or synchronized method ?

Posted On : 2014-12-23 14:23:42
Satisfied : 2 Yes  0 No
profile Adarsh S - anyforum.in Adarsh S
3100
Reply This Thread
up-rate
0
down-rate
Comments
One significant difference between synchronized method and block is that, Synchronized block generally reduce scope of lock. As scope of lock is inversely proportional to performance, it´s always better to lock only critical section of code. One of the best example of using synchronized block is double checked locking in Singleton pattern where instead of locking whole getInstance() method we only lock critical section of code which is used to create Singleton instance. This improves performance drastically because locking is only required one or two times.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  50050
Posted On :2014-12-23 15:21:30.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in