Hacker News new | ask | show | jobs
by tadfisher 1573 days ago
That's exactly what a Factory is for; you defer construction and pass around an object that knows how to create an instance. Then you're hunting at the root of your call tree to swap out a Factory instead of throughout your codebase to change a constructor call.

This is also what DI amounts to in practice. Frameworks abstract over it in the name of DRY, but at the same time introduce all the downsides of frameworks.

1 comments

A service still needs its own dependencies, thus forcing you to change its factories where needed. With a DI you re-define it in a single place.