Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə34/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   30   31   32   33   34   35   36   37   ...   47
Java Interview Guide

Dependency Injection
Inversion of Control
Inversion of Control (IoC) is a pattern that decouples software components from
their dependencies. For example, a password recovery service would need access
to a service that could deliver emails. A password service with control over its
dependencies would need to instantiate and configure an email service directly.
A password service with inverted control over its dependencies would have
an email service provided by some other object. A service that relinquishes
control of its dependencies is more flexible because different implementations
can be provided without changing any code in the service. For example, a mock
email service could be injected during testing to avoid sending emails or to
simulate a failure.


Service Locator vs Dependency Injection
Two common implementations of IoC are the service locator pattern and
dependency injection. A service locator provides a central repository for objects
to request dependencies. Dependency injection takes the inversion principle even
further by allowing a container to inject dependencies directly into components
through constructor arguments or setter methods.


Constructor Injection vs Setter Injection
Constructor injection requires a component to declare a constructor with
arguments for every dependency. This allows any initialization logic to run
in the constructor, but components with multiple dependencies may have
unwieldy constructor declarations. Setter injection requires a component
to declare setter methods for every dependency. Setter injection is more flexible,
but any initialization logic must take place after all the dependencies have been
injected. In either case, a container is required to manage the relationship
between components and their dependencies.



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   30   31   32   33   34   35   36   37   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin