5 ms·
> Factories are not a good thing. They usually imply that globals need to hold references. No. Your statements are fundamentally wrong at many levels. Factorie
by motorest 2y ago
> Factories are not a good thing. They usually imply that globals need to hold references.
No. Your statements are fundamentally wrong at many levels. Factories are tools to instantiate objects of a specific type a certain way. They are constructors that don't suffer from the limitations of constructors. A factory does not impose any requirement on object life cycle.
- lurking_swe 2y agocorrect. i chuckle when someone talks so authoritatively on a topic they didn’t try to fully understand in the first place.
- noduerme 2y agoHow you use a factory matters. A factory that sits as a static function inside the same class it's creating is one thing. It may just be a nicer interface to instantiate the class than the constructor itself. On the other hand, it may do something with the object it's instantiating before passing it back. Such as putting it into global scope, or tying it to something outside its chain. Factories that sit outside the class file are often prone to all kinds of grotesque later mucking-with that causes the instantiated object to bind to other scope, and never be properly garbage collected. That's what I'm talking about. Build better constructors and you won't usually need factories. If you need factories as an API, build them next to the constructor.