Should I use a factory method or call the constructor directly in my code?
#1
I'm trying to understand when it's appropriate to use a factory method versus just calling a constructor directly in my code. I keep refactoring a simple class that creates different configuration objects, and I can't decide if adding that extra layer is over-engineering or if it's the right move for maintenance.
Reply
#2
Been there. I started by creating config objects directly in constructors and it got messy fast when defaults changed. The moment we pushed building configs into a small helper layer, we could tweak defaults in one place and the rest kept working.
Reply
#3
We did add a simple factory and it did cut the mess at call sites. It helped newbies understand how to assemble the right config and stopped us from scattering default values. We still used constructors for tiny cases but the layer handled the shared pieces.
Reply
#4
Maybe the real issue is not the layer but how we decide which config to pick in tests. Is the indirection masking the real goal?
Reply
#5
Sometimes I drift off topic about naming and process while thinking about the same thing. I tried a factory approach for a single class and then the pattern leaked into something else and we had to rethink. I am not sure this is solving the right problem yet and I feel a bit stuck.
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: