I originally designed a "simple factory" to use type-checking and other methods to find out what kind of objects to produce. However, by my intuition, the more and more I go down this path the more it seems like it might be a bad design and that I should be using constants to produce the appropriate objects. I guess my question is, is this bad design and should I rewrite it? Any advice is much appreciated.
class WidgetFactory:
@staticmethod
def createWidget(raw_widget):
if isinstance(raw_widget, list):
return ListWidget(raw_widget)
elif FileWidget.isSupportedFile(raw_widget):
return FileWidget(raw_widget)
elif DBWidget.isSupportedDB(raw_widget):
return DBWidget(raw_widget)
As with all things, it depends.
If there will only be these 3 types, it's probably OK. If you will have to keep adding more types, then it's probably a poor design choice.
Having different methods for each type has the same problem since you will need to add methods for each type.
If you need to add more types later you might be able to use an AbstractFactory
or ChainOfResponsiblity
or something like that to find the appropriate implementation to use.