Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Static factory method doesn't work -- non-static does!

Reply
Thread Tools

Static factory method doesn't work -- non-static does!

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      05-22-2005
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
 
Reply With Quote
 
 
 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      05-22-2005
Matthias Kaeppler wrote:
> public static Product createProduct( Type t ) {
> switch( t ) {
> case( TYPE_1 ) return new Type1Product();
> case( TYPE_2 ) return new Type2Product();
> }
> }
> }
>


Erm, please ignore the bad syntax in the switch statement...
Of course I have it right here.
 
Reply With Quote
 
 
 
 
Vincent Vollers
Guest
Posts: n/a
 
      05-22-2005
Well, I dont know what you are doing exactly... but I wrote a small test
program, and it works perfectly:

the following code produces, as expected:

getNum in ProductTypeOne
1
getNum in ProductTypeTwo
2

===========================
abstract class Product {
protected int num;

abstract public int getNum();
}

class ProductTypeOne extends Product {
public ProductTypeOne() {
num = 1;
}

public int getNum() {
// TODO Auto-generated method stub
System.out.println("getNum in ProductTypeOne");
return num;
}
}

class ProductTypeTwo extends Product {
public ProductTypeTwo() {
num = 2;
}

public int getNum() {
// TODO Auto-generated method stub
System.out.println("getNum in ProductTypeTwo");
return num;
}
}

class TestContainer {
private Product product;

public TestContainer(Product p) {
this.product = p;
System.out.println(p.getNum());
}
}

class TestFactory {
enum Type {
TEST1,
TEST2
}

public static Product generateProduct(Type t) {
switch(t) {
case(TEST1): return new ProductTypeOne();
case(TEST2):
default: return new ProductTypeTwo();
}
}
}

public class TestProgram {

public TestProgram() {
new TestContainer(TestFactory.generateProduct(TestFact ory.Type.TEST1));
new TestContainer(TestFactory.generateProduct(TestFact ory.Type.TEST2));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestProgram();
}

}
===========================



 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-23-2005
On Sun, 2005-05-22 at 18:08 +0200, Matthias Kaeppler wrote:
> 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.


Sorry, your problem /is/ strange - works here as expected. However,
while playing around with it I came up with this, which I've not seen
anywhere before but has probably already been done...? It might help, or
it might not...

Imagine an Enum type:


// -----
enum AbstractClassType {
FIRST_CLASS (FirstClass.class),
SECOND_CLASS (SecondClass.class);
// ... etc

Class<? extends AbstractClass> classType;
ClassType(Class<? extends AbstractClass> theClass) {
classType = theClass;
}
public AbstractClass instantiate() throws InvocationTargetException {
try {
return classType.newInstance();
} catch (Exception e) {
throw(new InvocationTargetException(e));
}
}
}
// -----


And then imagine a base-class / factory:


// -----
abstract class AbstractClass {
public static AbstractClass getInstance(ClassType classType)
throws InvocationTargetException
{
return classType.instantiate();
}

public abstract Object someMethod(); // ... etc.
}
// -----


Cut out the middleman, so to speak. Of course you use it simply:

AbstractClass mine =
AbstractClass.getInstance(AbstractClassType.FIRST_ CLASS);

or whatever.

?

--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + http://www.velocityreviews.com/forums/(E-Mail Removed)in


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      05-23-2005
Matthias Kaeppler wrote:

> 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.


How about you post an example of what you're trying to do that _doesn't_
work. We can point out errors that you may be making in that case, but
when we only have an example of some similar thing that does work, we
can only speculate about what mistake you may have made in converting it.

--
John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-23-2005
On Mon, 23 May 2005 09:07:13 -0500, John C. Bollinger wrote:

> How about you post an example of what you're trying to do that _doesn't_
> work.


A sterling idea John. For more on that concept..
<http://www.physci.org/codes/sscce.jsp>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      05-23-2005
Andrew Thompson coughed up:
> On Mon, 23 May 2005 09:07:13 -0500, John C. Bollinger wrote:
>
>> How about you post an example of what you're trying to do that
>> _doesn't_ work.

>
> A sterling idea John. For more on that concept..
> http://www.physci.org/codes/sscce.jsp



Andrew, you should put a counter on that page. I'd like to see how many
times it's been visited.


--
"Realtor" and "realty" are pronounced "reel'-tor" and
"reel'-tee", *not* "reel'-a-tor" and "reel'-i-tee" !!!!
If you pronounce them when the extra syllable, you will
sound like a complete idiot.


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-24-2005
On Mon, 23 May 2005 20:16:04 GMT, Thomas G. Marshall wrote:

> Andrew Thompson coughed up:
>> On Mon, 23 May 2005 09:07:13 -0500, John C. Bollinger wrote:
>>
>>> How about you post an example of what you're trying to do that
>>> _doesn't_ work.

>>
>> A sterling idea John. For more on that concept..
>> http://www.physci.org/codes/sscce.jsp

>
> Andrew, you should put a counter on that page.


I have considered using counters at one time or another, but resisted
(thus far) for two reasons.
1) They are inherently inaccurate (with caching + 100 other factors)
2) They are considered (in web design circles) to mark a page as made
by an 'enthusiastic amateur'.

> ..I'd like to see how many times it's been visited.


OK.. Just checked the PhySci.org server stats for April 2005.

In the list of 'Top 30 of 1059 Total URLs' recorded as visited,
the SSCCE document comes in at 27th with 867 hits.

Thoughts, ..comments?

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-24-2005
On Tue, 2005-05-24 at 00:12 +0000, Andrew Thompson wrote:
> > ..I'd like to see how many times it's been visited.

>
> OK.. Just checked the PhySci.org server stats for April 2005.
>
> In the list of 'Top 30 of 1059 Total URLs' recorded as visited,
> the SSCCE document comes in at 27th with 867 hits.
>
> Thoughts, ..comments?
>


If it's not the posters reading it, then who is?

"I have this problem in some code that kind of goes...
IF this THEN do this ..."

>8-|


--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + (E-Mail Removed)in


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-24-2005
On Tue, 24 May 2005 02:03:41 +0100, Ross Bamford wrote:

> On Tue, 2005-05-24 at 00:12 +0000, Andrew Thompson wrote:


(T.G.M.)
>>> ..I'd like to see how many times it's been visited.

>>
>> OK.. Just checked the PhySci.org server stats for April 2005.
>>
>> In the list of 'Top 30 of 1059 Total URLs' recorded as visited,
>> the SSCCE document comes in at 27th with 867 hits.

....
> If it's not the posters reading it, then who is?


Aaahh! The first signs of why you should treat such numbers
with extreme caution. What does 'reading' mean? It is certainly
a subset of 'hits'..

The figure of 867 -hits- includes every time the page was called from
the server, including the times ..
- I myself checked the exact wording (of some part of it) prior to
posting the link in reponse to a question.
- The client was a search engine spider.
- Visitors clicked the link and then thought, 'No - this isn't what I
was after!' and left the page.
- Visitors read part of the page but got bored, called away, had
power failure..
- Visitors that read the entire page but failed to understand it, or
it's deeper message.

So, in the end, these numbers amount to
"lies, damned lies, ..and statistics!"

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
Conundrum with package name in "dynamic" static factory method Nobody Java 6 07-14-2005 04:50 PM
A Java Brainteaser - a Static Factory method Narrative krasicki Java 69 05-30-2005 05:55 PM
java static factory method vs. constructor - object reuse javaguy44 Java 6 09-22-2004 05:00 PM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments