I have noticed a very unpleasant trend on the forums, with dependency injection growing in popularity it is being applied to everything. Factories and the plain old new are still valid constructional patterns. Things like exceptions and domain objects can be created with them.
Plain Old New is good for objects whose type is fairly fixed and has limited and equally simple dependencies. Common object include data transfer objects, domain objects and exceptions.
Factories are good for objects who need to be created at runtime with a set of parameters. Factories can work hand-in-hand with DI (i.e. the factory could use the Injector in Guice, or take a set of Providers).
Dependency Injection builds the guts of your application. It understands the dependency tree of each object it creates and handles it all for you. Data access objects, services, controllers and views are all items that are created and handled by the DI framework. It will also handle scopes (limit the number of instances and which instance of a object is given at each point in the tree).
No comments:
Post a Comment