M
Matthias Kaeppler
Howdy,
I have a strange problem:
I want to implement a factory class, which has a static creation method
taking an enum type which defines what type will be created.
The problem is, as soon as I /do/ make this method static, the program
behaves very strange, in that calls on the returned object are never
executed.
Here's some code for illustration:
class Factory {
enum Type {
TYPE_1,
TYPE_2
}
public static Product createProduct( Type t ) {
switch( t ) {
case( TYPE_1 ) return new Type1Product();
case( TYPE_2 ) return new Type2Product();
}
}
}
Type1Product and Type2Product extend Product.
Now I somewhere pass the reference to the newly created object to some
other object's ctor:
Foo obj = new Foo( Factory.createProduct( Factory.Type.TYPE_1 ) );
Inside obj, all calls on the object returned by the factory method fail,
in that they are never executed!
However, if I make the factory method non-static, and instantiate the
whole Factory class first, then everything works just fine!
I can't even remotely imagine what is causing this, can you?
Any help greatly appreciated.
Cheers,
Matthias
I have a strange problem:
I want to implement a factory class, which has a static creation method
taking an enum type which defines what type will be created.
The problem is, as soon as I /do/ make this method static, the program
behaves very strange, in that calls on the returned object are never
executed.
Here's some code for illustration:
class Factory {
enum Type {
TYPE_1,
TYPE_2
}
public static Product createProduct( Type t ) {
switch( t ) {
case( TYPE_1 ) return new Type1Product();
case( TYPE_2 ) return new Type2Product();
}
}
}
Type1Product and Type2Product extend Product.
Now I somewhere pass the reference to the newly created object to some
other object's ctor:
Foo obj = new Foo( Factory.createProduct( Factory.Type.TYPE_1 ) );
Inside obj, all calls on the object returned by the factory method fail,
in that they are never executed!
However, if I make the factory method non-static, and instantiate the
whole Factory class first, then everything works just fine!
I can't even remotely imagine what is causing this, can you?
Any help greatly appreciated.
Cheers,
Matthias