Skip to content

Commit 0c0d84c

Browse files
committed
Added source code comments.
1 parent fdf1d14 commit 0c0d84c

File tree

10 files changed

+71
-0
lines changed

10 files changed

+71
-0
lines changed

dependency-injection/src/main/java/com/iluwatar/AdvancedWizard.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* AdvancedWizard implements inversion of control.
6+
* It depends on abstraction that can be injected through
7+
* its constructor.
8+
*
9+
*/
310
public class AdvancedWizard implements Wizard {
411

512
private Tobacco tobacco;

dependency-injection/src/main/java/com/iluwatar/App.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
import com.google.inject.Guice;
44
import com.google.inject.Injector;
55

6+
/**
7+
*
8+
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
9+
* implements so called inversion of control principle. Inversion of control has two specific rules:
10+
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
11+
* - Abstractions should not depend on details. Details should depend on abstractions.
12+
*
13+
* In this example we show you three different wizards. The first one (SimpleWizard) is a naive
14+
* implementation violating the inversion of control principle. It depends directly on a concrete
15+
* implementation which cannot be changed.
16+
*
17+
* The second wizard (AdvancedWizard) is more flexible. It does not depend on any concrete implementation
18+
* but abstraction. It utilizes Dependency Injection pattern allowing its Tobacco dependency to be
19+
* injected through its constructor. This way, handling the dependency is no longer the wizard's
20+
* responsibility. It is resolved outside the wizard class.
21+
*
22+
* The third example takes the pattern a step further. It uses Guice framework for Dependency Injection.
23+
* TobaccoModule binds a concrete implementation to abstraction. Injector is then used to create
24+
* GuiceWizard object with correct dependencies.
25+
*
26+
*/
627
public class App {
728

829
public static void main( String[] args ) {

dependency-injection/src/main/java/com/iluwatar/GuiceWizard.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import javax.inject.Inject;
44

5+
/**
6+
*
7+
* GuiceWizard implements inversion of control.
8+
* Its dependencies are injected through its constructor
9+
* by Guice framework.
10+
*
11+
*/
512
public class GuiceWizard implements Wizard {
613

714
private Tobacco tobacco;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* OldTobyTobacco concrete Tobacco implementation
6+
*
7+
*/
38
public class OldTobyTobacco extends Tobacco {
49
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* RivendellTobacco concrete Tobacco implementation
6+
*
7+
*/
38
public class RivendellTobacco extends Tobacco {
49
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* SecondBreakfastTobacco concrete Tobacco implementation
6+
*
7+
*/
38
public class SecondBreakfastTobacco extends Tobacco {
49
}

dependency-injection/src/main/java/com/iluwatar/SimpleWizard.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Naive Wizard implementation violating the inversion of control principle.
6+
* It should depend on abstraction instead.
7+
*
8+
*/
39
public class SimpleWizard implements Wizard {
410

511
private OldTobyTobacco tobacco = new OldTobyTobacco();

dependency-injection/src/main/java/com/iluwatar/Tobacco.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Tobacco abstraction
6+
*
7+
*/
38
public abstract class Tobacco {
49

510
public void smoke(Wizard wizard) {

dependency-injection/src/main/java/com/iluwatar/TobaccoModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.google.inject.AbstractModule;
44

5+
/**
6+
*
7+
* Guice module for binding certain concrete Tobacco implementation.
8+
*
9+
*/
510
public class TobaccoModule extends AbstractModule {
611

712
@Override

dependency-injection/src/main/java/com/iluwatar/Wizard.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Wizard interface
6+
*
7+
*/
38
public interface Wizard {
49

510
void smoke();

0 commit comments

Comments
 (0)