开发者

Java Factory Method/ Singleton pattern

开发者 https://www.devze.com 2023-02-06 09:16 出处:网络
Why is it a standard deign to implement Factory classes as singleton? What is wrong with this: public class Factory{

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消