Victor
Victor

Reputation: 17107

Java Factory Method/ Singleton pattern

Why is it a standard deign to implement Factory classes as singleton? What is wrong with this:

public class Factory{

public static createObjects(ObjectArgs arg){

return new Object(arg);
}
}
----
public class FactoryClient{

public void someMethod(){

Factory.createObjects(ObjectArgs arg);

}
}

Upvotes: 0

Views: 783

Answers (2)

TrueWill
TrueWill

Reputation: 25563

There's nothing* wrong with creating static factory methods, and I see it fairly often.

* - That is, nothing wrong that isn't wrong with any static method (coupling, lack of testability, etc.). Consider using an IoC Container and dependency injection instead of factories.

Upvotes: 0

Bozho
Bozho

Reputation: 597422

This is not a singleton. It is a factory-method (it's not even a factory). There is nothing wrong in having a factory-method like that.

Upvotes: 2

Related Questions