Factory design question

Is it gospel that a Factory be static? For instance, I'd like to have a Factory class that I maintain a reference to and returns me the correct instance of an object, which I invoke methods on. However, I want the Factory class to hold onto all the references while I have another object which maintains the reference to the Factory. I know this is possible, but I don't know if I'm violating a best practice here. Every time I read about an Abstract Factory, it is used with a static reference. Or is this a completely different kind of design pattern?

[563 byte] By [Bart69a] at [2007-11-26 14:45:00]
# 1
Rethink your design: do you want your factory to deliver the same productover and over again? If so, your factory should be a container of singletons,if not, what's the problem?kind regards,Jos
JosAHa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 2
I'm not clear on what you are saying: are you asking if a factory always should be a Singleton?
DrLaszloJamfa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 3

No, for instance, I'd like it to be this way. Assume Dog and Cat inherit Animal and have all the required methods.

MyObject will maintain a reference to the Factory, which will in turn maintain all my necessary references. The client code will only call feedAnimal or walkAnimal on MyObject. My question is, is this poor design or violating any best practices?

public class MyObject {

AnimalFactory af;

public MyObject() {

af = new AnimalFactory();

}

private Animal getAnimal(String name) {

Animal al = af.getInstance(name);

return al;

}

public void feedAnimal(String name) {

getAnimal(name).feed();

}

public void walkAnimal(String name) {

getAnimal(name).walk();

}

}

public class AnimalFactory {

public AnimalFactory() {

}

public Animal getInstance(String s) {

Animal al;

if (s.equals(ROVER)) {

al = new Dog();

} else if (s.equals(WHISKERS)) {

al = new Cat();

}

return al;

}

}

Bart69a at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 4
> I'm not clear on what you are saying: are you asking> if a factory always should be a Singleton?Nope;kind regards,Josps. You weren't asking me were you?
JosAHa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 5
Everytime you call feedAnimal or walkAnimal you create a new dog or cat. Is that what you want?
DrLaszloJamfa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 6

Not exactly. Its actually my fault, I left out some code. The Factory will keep local copies of the instances and only create one instance of each Dog and Cat per each instance of the Factory. Such as:

public class AnimalFactory {

Dog d;

Cat c;

public AnimalFactory() {

}

public Animal getInstance(String s) {

Animal al;

if (s.equals(ROVER)) {

if (d==null) {

d = new Dog();

}

al = d;

} else if (s.equals(WHISKERS)) {

if (c==null) {

c = new Cat();

}

al = c;

}

return al;

}

}

Bart69a at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 7

Hmmm... that "one each of each type" is contrived, but your factory is doing two things that are common to factories:

1. creating objects on request

2. maintaining references to the objects it creates.

That second point can get you into memory issues if the objects accumulate with no consideration for how to get rid of the loiterers.

DrLaszloJamfa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 8

> That second point can get you into memory issues if

> the objects accumulate with no consideration for how

> to get rid of the loiterers.

I'd suggest WeakReferrence or SoftReference for this.

Man, I feel like a broken record. That must be my 5th time in a week.

:)

es5f2000a at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 9

> I'd suggest WeakReferrence or SoftReference for this.

> Man, I feel like a broken record. That must be my

> 5th time in a week.

I was going to suggest weak references, too, but his code looks like it's counting on the factory to maintain a proper reference to each animal, since no references are maintained elsewhere. In any case, it's a design issue that should be sorted out.

DrLaszloJamfa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 10

I am definitely counting on the Factory to maintain a proper reference to each Animal. Basically, every instance of the Factory should only contain one instance of the Animals inside. And I plan on adding code to handle the loiterers. I know its not perfect, but is this considered an OK design? I want to have one central place to maintain the instances, but the idea of me also instantiating the Factory itself had me skeptical. I've always seen this done through static means.

Bart69a at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...
# 11
Separate from any concerns about the products (animals), it's common for the factory to be a Singleton.
DrLaszloJamfa at 2007-7-8 8:32:39 > top of Java-index,Java Essentials,Java Programming...