Journal cyranoVR's Journal: Java: A Verbose Acronym 9
Can you spot the error?
import some.imaginary.package.MyService;
public class MyServiceFactory {
private static MyService instance;
String factoryName;
public MyServiceFactory(String factoryName) {
this.factoryName = factoryName;
}
public Object getService() {// create if null...
synchronized(this) {
if(MyServiceFactory.instance == null) {
MyServiceFactory.instance = new MyService();
}
}
return MyServiceFactory.instance;
}
public static void main(String args[]) {
Thread a = new Thread() {
public void run() {
MyService service = new MyServiceFactory("a").getService();
service.doStuff();
}
};
Thread a = new Thread() {
public void run() {
MyService service = new MyServiceFactory("b").getService();
service.doStuff();
}
};
a.start();
b.start();
}
}
The error is not a syntax one, so if I'm missing a semi-colon or something, please assume that's unintentional.
Found one (Score:1)
Shouldn't it be MyServiceFactory.instance?
Should this be MyServiceFactory("a").getObject(); ?
WTF why even have a factory here? Why not just go MyService service = new MyService(); You lose track of that MyServiceFactory("a") right outside th
Re:Found one (Score:2)
My shot at it.... (Score:1)
It's been a while I did Java, so I may be way off.
The signature of getService(), specifies that it returns an Object. Okay, but further in the code you find the following lines:
Since, getService() returns an Object and this Object is assigned to a more specialized class, it should be cast to said class.
The second thing that I find strange is the staticness of "instance". Why? Q
Re:My shot at it.... (Score:2)
http://java.sun.com/j2se/1.4.2/docs/api/javax/nam
This is a cornerstone of J2EE
Corrected link (Score:2)
Jaw is right (Score:1)
I still say the other idiocy is using a factory class at all! Factory classes are good when you have complication associated with creating an object that goes beyond just a constructor. Have the object implement runnable and be done with it.
Re:Jaw is right (Score:2)
Hint: concurrency.
Re:Jaw is right (Score:1)
Honestly, never had to fart around with JNDI, just heard bad things about it.
Ignore this post, tired.
Re:Jaw is right (Score:2)
http://www.javaworld.com/javaworld/jw-04-2003/jw-
http://tomcat.apache.org/tomcat-5.0-doc/jndi-reso