• Dear Guest, Please do NOT forget to join our JAVA JOBS (ONLY) FACEBOOK GROUP to be aware of latest JAVA openings, and you may also post any JAVA related queries/jobs.

Program for Singleton class

#1
Hi Friends,
I would like to share one senarion of sigleton design pattern which asked by one interviwer.i hope will get answer in this forum.if any one can,plz solve it.
i know how to create sigelton but analyze bellow senario
Senario:
I want to create five object of a class and when i call 6th object it should be return 1st object and when i call 7th object it should be return 2nd object like this if we call 8th object it should be return 3rd object and so on......upt to10th object should be return 5th object.

I hope u got my point,so please provide me help ASAP to write the code
Thanx Advance

Regards,
gyan
 
#3
Re: Sigleton DP on senario!!!!!Help PLZ

Hi Friends,
I would like to share one senarion of sigleton design pattern which asked by one interviwer.i hope will get answer in this forum.if any one can,plz solve it.
i know how to create sigelton but analyze bellow senario
Senario:
I want to create five object of a class and when i call 6th object it should be return 1st object and when i call 7th object it should be return 2nd object like this if we call 8th object it should be return 3rd object and so on......upt to10th object should be return 5th object.

I hope u got my point,so please provide me help ASAP to write the code
Thanx Advance

Regards,
gyan
*******************************************************

Dear Gyan,
I am Mohd Shakir 25 years old by profession I am a Java Developer.Solution of your discussed scenario is:---


//Singleton class

package single;

public class Singleton
{
private static int i;
static Singleton single;
private Singleton()
{
System.out.println("This is "+i+" object");
}

public static Singleton createInstance()
{
if(i==5)
{
i=1;
single=new Singleton();
}
else
{
i+=1;
single=new Singleton();
}
return single;
}
}


//main class

package main;

import single.Singleton;

public class SingletonDemo
{
public static void main(String s[])
{
for(int i=0;i<10;i++)
{
Singleton.createInstance();
}
}
}


just copy, paste, run and see the result.

Regards.

Mohd Shakir

get2shakir23@yahoo.com
 
#4
Re: Sigleton DP on senario!!!!!Help PLZ

Thanx alot Md. Shakir
I am very happy to get this ans.
i had idea of this, itried also but i could't implement.
now i getting more confidence in this forum.which will explore myself.
I was too dipress, i haven't get answer's of my single such type of questions.
it is most awaited problem.

rgds,
gyan
 
#5
public class Test1 {

private Test1(){};
public static int counter = 0;

public static Test1 test1 = null;
public static Test1 test2 = null;
public static Test1 test3 = null;
public static Test1 test4 = null;
public static Test1 test5 = null;

public static Test1 getInstance() {
Test1 testResult = null;
if (counter <= 5) {

switch (counter) {
case 1:
test1 = new Test1();
testResult = test1;
System.out.println("1");
break;
case 2:
test2 = new Test1();
testResult = test2;
System.out.println("2");
break;
case 3:
test3 = new Test1();
testResult = test3;
System.out.println("3");
break;
case 4:
test4 = new Test1();
testResult = test4;
System.out.println("4");
break;
case 5:
test5 = new Test1();
testResult = test5;
System.out.println("5");
break;
}

counter++;
if(counter > 5){

counter = counter - 5;
}
}

return testResult;

}

}
 
#6
Re: Sigleton DP on senario!!!!!Help PLZ

Hello Mohd Shakir

Your program print the correct values because you reset the value of i when it reaches 5. But still it creates new object of the Singleton class. If you print the hash code of the objects in SingletonDemo class, then you can see that all objects has different has code and hence 6th object hash code is not equal to 1st object, 7th object has code is not equal to 2nd object and so on.

Please check out the below code

public class SingletonDemo {

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Singleton instance = Singleton.createInstance();
System.out.println(instance.hashCode());
}
}
}

class Singleton {

private static int counter = 0;

private static Singleton[] singletonObj = new Singleton[5];

private Singleton() {
}

public static Singleton createInstance() {
if (singletonObj[counter] == null) {
singletonObj[counter] = new Singleton();
}
Singleton singleton = singletonObj[counter];
counter++;
if (counter == 5) {
counter = 0;
}
return singleton;
}
}
 
Top