regarding creation of objects

hello all,

i just wan to know about how can i restrict object creation ... ( not by hard coding...)

suppose i want to create only two objects can use single tone design pattern for this( i know that we can create only one by using this design pattern)... is there any possibility to use this design pattern.... for this task..

[347 byte] By [sricharana] at [2007-11-27 9:39:19]
# 1
You can modify the singleton pattern so that it checks how many instances that it has created.Kaj
kajbja at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 2
hello kaj,thanks for quick reply...i am trying to get this only can you explain briefly please.....
sricharana at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 3
You need to be more specific in that case. Why do you need two instances? What will those instances do?
kajbja at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 4
without hard-coding?
petes1234a at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm curious: if you have two instances, and you ask for one, how do you decide which one is wanted?Seems to me the simplest way is to have a two element array as the singleton object.
malcolmmca at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 6
> without hard-coding?look up number of instances to create in a properties file?
curry_monstera at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 7

Have you understood how the Singleton pattern works? What is your problem when you try to modify it to accept two instances instead of one?

The best design depends on the details of your requirements. Like, for a client, whether it matters which of the two instances it gets. How magic the number 2 is; if there's any possibility that in a future application of your class, it might be 1 or 3 or 200 instances, or it always will be 2.

OleVVa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 8
Two is the only even prime. Hardly magic. Two presidents named Bush?
BigDaddyLoveHandlesa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 9
Mmmm... curried monster...
BigDaddyLoveHandlesa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 10
You can create a int static variable and check the the value in the constructor.
manuel.leiriaa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 11

> You can create a int static variable and check the

> the value in the constructor.

In the constructor? Probably a poor idea. What would you do there if the count hits 3?

Better do it in the getInstance method (or what you call the public method (or methods?) that returns one of the two instances. If the count is already two, return one of the already created instances (or null, depending on the requirements).

OleVVa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 12
> In the constructor? Probably a poor idea. What would> you do there if the count hits 3?throw some exception
manuel.leiriaa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 13

And declare getInstance can throw an exception?

Or throw an unchecked exception and risk your program crashes when somebody tries to create a 3rd object?

I like your counter idea very much. I still prefer to check the counter in getInstance() rather than in the constructor.

Yes, there are more options than the two I mentioned. They share a tendency to make the design more complicated.

OleVVa at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 14

thanks to my community people for your response...

Hello OleW,

here is one single tone class, i also got that but unable to implement.... see this whether it's able to get or not....

public class SingleT{

private static SingleT Object.uniqueInst[] ;

private int count=0;

private SingleT() {}

public static final SingleT getInstance() {

for(i=0;i<=count;i++){

if (uniqueInst == null )

uniqueInst = new SingleT();

count++;

}

return uniqueInst;

}

}

sricharan

sricharana at 2007-7-12 23:14:23 > top of Java-index,Java Essentials,Java Programming...
# 15

I'm not sure if it works. Give it a try

public class ClassicSingleton {

private static ClassicSingleton instance0 = null;

private static ClassicSingleton instance1 = null;

private ClassicSingleton() {

}

public static ClassicSingleton getInstance() {

if(instance0 == null) {

instance0 = new ClassicSingleton();

return instance0;

}else if(instance1 == null{

instance1 = new ClassicSingleton();

return instance1;

}else{

//return null or throw some exception

}

}

}

Manuel Leiria

manuel.leiriaa at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 16
That is not thread safe and even if it were it seems you could only call getInstance twice.This whole idea of having two instances seems ridiculous to me.
YoGeea at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 17

> That is not thread safe

Now is thread safe

public class ClassicSingleton {

private static ClassicSingleton instance0 = null;

private static ClassicSingleton instance1 = null;

private ClassicSingleton() {

}

public synchronized static ClassicSingleton getInstance() {

if(instance0 == null) {

instance0 = new ClassicSingleton();

return instance0;

}else if(instance1 == null{

instance1 = new ClassicSingleton();

return instance1;

}else{

//return null or throw some exception

//return instance0 or instance1 ?

}

}

}

>and even if it were it seems

> you could only call getInstance twice.

I guess you're right!

> This whole idea of having two instances seems

> ridiculous to me.

I agree with you, having two instances smells like bad design

Message was edited by:

manuel.leiria

manuel.leiriaa at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 18

> That is not thread safe and even if it were it seems

> you could only call getInstance twice.

Fill in either return instance0; or return instance1; in the latter else part, and you can call as many times as you want.

> This whole idea of having two instances seems

> ridiculous to me.

I was hoping the OP would explain the requirements and the reason for them a little more. We cannot rule out there's a point it it. Not that I have any idea what the point might be.

"4. Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of a Singleton class. " (Gamma et al.: Design Patterns, page 128, subsection "Consequences")

OleVVa at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 19
EDIT: never mind
YoGeea at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 20

hello java community,

Thanks for all to spend your valuable time .... as one of the member said (in example program) it checks whether the first object is created or not then it goes to the second one... but as per my requirment it must create the two objects when invoked.....

please throw your suggestions... please....

sricharana at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...
# 21
I hope this doesn't sound rude: I suggest you make the first attempt yourself, then tell us what problems you have, and we'll all be here to help solve them. I mean, writing the Java code to create two objects, is that difficult? There may be other difficulties; you tell
OleVVa at 2007-7-21 23:05:26 > top of Java-index,Java Essentials,Java Programming...