This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
github-actions
committed
Jan 9, 2024
1 parent
c2e9528
commit 83648ae
Showing
9 changed files
with
1,527 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...t 02aeaf9ec9954f86bd08ea3bd620f04a/Patterns c9fbf0af8e1648ee80d4ddedd63983f1.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Patterns | ||
|
||
[Dependency Injection](Patterns%20c9fbf0af8e1648ee80d4ddedd63983f1/Dependency%20Injection%207193bc478cee4995b7538edcfbcc189e.md) |
106 changes: 106 additions & 0 deletions
106
...1648ee80d4ddedd63983f1/Dependency Injection 7193bc478cee4995b7538edcfbcc189e.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Dependency Injection | ||
|
||
# Types | ||
|
||
There are three types of Dependency Injection: | ||
|
||
- Type 1 IoC (Constructor Injection) | ||
- Type 2 IoC (Setter Injection) | ||
- Type 3 IoC (Interface Injection) | ||
|
||
## Constructor Injection | ||
|
||
[As Martin Fowler describes constructor injection](https://martinfowler.com/articles/injection.html), there is mostly a sort of assembler or container system involved where you can register implementations used for specific interfaces and where dependencies and dependents are defined. | ||
|
||
Then, the actual dependency is passed into a constructor of a class using an interface defining the dependencies behavior. | ||
|
||
![[Source: Martin Fowler](https://martinfowler.com/articles/injection/injector.gif)](Dependency%20Injection%207193bc478cee4995b7538edcfbcc189e/Untitled.png) | ||
|
||
[Source: Martin Fowler](https://martinfowler.com/articles/injection/injector.gif) | ||
|
||
``` | ||
class MovieLister... | ||
public MovieLister(MovieFinder finder) { | ||
this.finder = finder; | ||
} | ||
class ColonMovieFinder... | ||
public ColonMovieFinder(String filename) { | ||
this.filename = filename; | ||
} | ||
private MutablePicoContainer configureContainer() { | ||
MutablePicoContainer pico = new DefaultPicoContainer(); | ||
Parameter[] finderParams = {new ConstantParameter("movies1.txt")}; | ||
pico.registerComponentImplementation(MovieFinder.class, ColonMovieFinder.class, finderParams); | ||
pico.registerComponentImplementation(MovieLister.class); | ||
return pico; | ||
} | ||
``` | ||
|
||
## Setter Injection | ||
|
||
```go | ||
type MovieFinder struct { | ||
lister MovieLister | ||
} | ||
|
||
func (t *MovieFinder) SetLister(lister MovieLister) { | ||
t.lister = lister | ||
} | ||
``` | ||
|
||
## Interface Injection | ||
|
||
Almost the same as setter injection, but the injector method is specified by an interface. | ||
|
||
```go | ||
public interface ServiceSetter { | ||
void setService(Service service); | ||
} | ||
|
||
public class Client implements ServiceSetter { | ||
private Service service; | ||
|
||
@Override | ||
public void setService(Service service) { | ||
if (service == null) { | ||
throw new IllegalArgumentException("service must not be null"); | ||
} | ||
this.service = service; | ||
} | ||
} | ||
|
||
public class ServiceInjector { | ||
private final Set<ServiceSetter> clients = new HashSet<>(); | ||
|
||
public void inject(ServiceSetter client) { | ||
this.clients.add(client); | ||
client.setService(new ExampleService()); | ||
} | ||
|
||
public void switch() { | ||
for (Client client : this.clients) { | ||
client.setService(new AnotherExampleService()); | ||
} | ||
} | ||
} | ||
|
||
public class ExampleService implements Service {} | ||
|
||
public class AnotherExampleService implements Service {} | ||
``` | ||
|
||
# Resources | ||
|
||
## Articles | ||
|
||
- [https://martinfowler.com/articles/injection.html](https://martinfowler.com/articles/injection.html) | ||
- [https://en.wikipedia.org/wiki/Dependency_injection](https://en.wikipedia.org/wiki/Dependency_injection) | ||
|
||
## Videos | ||
|
||
- [https://youtu.be/J1f5b4vcxCQ](https://youtu.be/J1f5b4vcxCQ) | ||
- [https://youtu.be/yunF2PgJlHU](https://youtu.be/yunF2PgJlHU) |
Binary file added
BIN
+10.8 KB
...ddedd63983f1/Dependency Injection 7193bc478cee4995b7538edcfbcc189e/Untitled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.