Using Factories to Create Dependencies
If we have a dependency that needs configuration, it doesn’t always make sense to inject that
dependency itself. Sometimes it might be a better idea to inject an object that knows how to build
this dependency for us. This is useful when the way in which that dependency is configured is
controlled by scenarios within the class itself.
Looking at the example above: what if we wanted to return a different type of response based
on a context requested? Maybe by default we would return HTML, but based on context, might
return JSON or XML as well?
In this case, we may want to inject a factory that knows how to build the response based on the
requested context:
Do'stlaringiz bilan baham: |