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);
}
}
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.
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.
精彩评论