Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grasp creator vs. dependency Injection

Is GRASP Creator a complete contradiction to Dependency Injection?

If it is not, please explain why.

like image 823
Ols1 Avatar asked Nov 04 '22 22:11

Ols1


1 Answers

It is not a Contradiction, however they are both used in different circumstances.

You are more likely to use Creator in the Domain Layer, and use Dependency Injection or Factories in your Application Service Layers (logging service, controller factories, authentication services, etc. depending on your platform) and Domain Service / Data Access Layers (Repositories, Data Mappers, Data Gateways, etc.).

This distinction is made clear by Larman in "Applying UML and Patterns" (where GRASP Patterns/Principles are defined) stating that Creator is contraindicated in instances where reuse of recycled instances are important for performance or the decision as to which class among a family needs to be instantiated. In these instances, instantiation is better left relegated to Factory, Abstract Factory, or DI.

like image 76
Ethan Cabiac Avatar answered Nov 09 '22 08:11

Ethan Cabiac