java - Should Factory Class' create() method be static? Or make Factory Class singleton? -
title pretty sums up.
i have factory class construct instance of class when it's create() method called. have setup factory class singleton, forces invokation such:
someclass clazz = (someclass) factoryclass.getinstance().create(enum.type); this guarantees factory thread safe, may cause blocking, etc.
if factory not singleton, instead set .create() method static, achieve similar results.
someclass clazz = (someclass) factoryclass.create(enum.type); assuming multiple threads may attempt create instances of objects via factoryclass, better? don't both ways of doing provide same level of thread safety?
neither has impact on thread-safety. threading issues reside in create method's implementation. static method create instance (or singleton) , call instance method there no difference.
exposing singleton api choice in how want , feel.
that said, static methods more challenging mock in testing environment might consider.
Comments
Post a Comment