Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Because it allows you to mock the dependency when you are unit testing. And if your using dependency injection it allows you to switch out the implementation if needed without having to find all hard coded references. Separating the implementation from its use is always good practice.
-
@halfflat if your doing it correctly you do. A factory is to create instances of the object. Your separating the creation from its use and it allows you to dynamically switch out the resource at runtime if needed. It’s always best practice to use factories, repositories, facades or any other well established pattern as they help make the application loosely coupled and helps you maintain SOILID & DRY implementations
-
@halfflat think you need to read some design pattern books coz that’s not how it works I’m afraid. Also DI should be the thing that’s ultimately responsible for your object creation and it’s lifetime
-
Pragmatism should rule. If you think there's a chance of extension/different implementations/etc, then start with whatever pattern you deem appropriate. Otherwise, go simple.
Related Rants
Jesus fuck. Why does anyone thing you need a factory pattern when you have only one single class you create...
question
rant