Reformat business-delegate, callback, chain, command, composite, dao, decorator & dependency-injection patterns.

This commit is contained in:
Ankur Kaushal
2015-11-01 18:48:43 -05:00
parent 3af06a3a3a
commit 449340bd2b
54 changed files with 698 additions and 700 deletions

View File

@ -2,21 +2,20 @@ package com.iluwatar.dependency.injection;
/**
*
* AdvancedWizard implements inversion of control.
* It depends on abstraction that can be injected through
* its constructor.
* AdvancedWizard implements inversion of control. It depends on abstraction that can be injected
* through its constructor.
*
*/
public class AdvancedWizard implements Wizard {
private Tobacco tobacco;
public AdvancedWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
private Tobacco tobacco;
@Override
public void smoke() {
tobacco.smoke(this);
}
public AdvancedWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
}

View File

@ -5,40 +5,41 @@ import com.google.inject.Injector;
/**
*
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
* implements so called inversion of control principle. Inversion of control has two specific rules:
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
* - Abstractions should not depend on details. Details should depend on abstractions.
* <p>
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a naive
* implementation violating the inversion of control principle. It depends directly on a concrete
* implementation which cannot be changed.
* <p>
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete implementation
* but abstraction. It utilizes Dependency Injection pattern allowing its {@link Tobacco} dependency to be
* injected through its constructor. This way, handling the dependency is no longer the wizard's
* responsibility. It is resolved outside the wizard class.
* <p>
* The third example takes the pattern a step further. It uses Guice framework for Dependency Injection.
* {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then used to create
* {@link GuiceWizard} object with correct dependencies.
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
* - Abstractions should not depend on details. Details should depend on abstractions.
* <p>
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a
* naive implementation violating the inversion of control principle. It depends directly on a
* concrete implementation which cannot be changed.
* <p>
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
* implementation but abstraction. It utilizes Dependency Injection pattern allowing its
* {@link Tobacco} dependency to be injected through its constructor. This way, handling the
* dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
* <p>
* The third example takes the pattern a step further. It uses Guice framework for Dependency
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
* used to create {@link GuiceWizard} object with correct dependencies.
*
*/
public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main( String[] args ) {
SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke();
Injector injector = Guice.createInjector(new TobaccoModule());
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
guiceWizard.smoke();
}
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke();
Injector injector = Guice.createInjector(new TobaccoModule());
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
guiceWizard.smoke();
}
}

View File

@ -4,22 +4,21 @@ import javax.inject.Inject;
/**
*
* GuiceWizard implements inversion of control.
* Its dependencies are injected through its constructor
* by Guice framework.
* GuiceWizard implements inversion of control. Its dependencies are injected through its
* constructor by Guice framework.
*
*/
public class GuiceWizard implements Wizard {
private Tobacco tobacco;
@Inject
public GuiceWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
private Tobacco tobacco;
@Inject
public GuiceWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
}

View File

@ -2,15 +2,15 @@ package com.iluwatar.dependency.injection;
/**
*
* Naive Wizard implementation violating the inversion of control principle.
* It should depend on abstraction instead.
* Naive Wizard implementation violating the inversion of control principle. It should depend on
* abstraction instead.
*
*/
public class SimpleWizard implements Wizard {
private OldTobyTobacco tobacco = new OldTobyTobacco();
public void smoke() {
tobacco.smoke(this);
}
private OldTobyTobacco tobacco = new OldTobyTobacco();
public void smoke() {
tobacco.smoke(this);
}
}

View File

@ -6,8 +6,9 @@ package com.iluwatar.dependency.injection;
*
*/
public abstract class Tobacco {
public void smoke(Wizard wizard) {
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName()));
}
public void smoke(Wizard wizard) {
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this
.getClass().getSimpleName()));
}
}

View File

@ -9,8 +9,8 @@ import com.google.inject.AbstractModule;
*/
public class TobaccoModule extends AbstractModule {
@Override
protected void configure() {
bind(Tobacco.class).to(RivendellTobacco.class);
}
@Override
protected void configure() {
bind(Tobacco.class).to(RivendellTobacco.class);
}
}

View File

@ -6,7 +6,7 @@ package com.iluwatar.dependency.injection;
*
*/
public interface Wizard {
void smoke();
void smoke();
}

View File

@ -11,9 +11,9 @@ import com.iluwatar.dependency.injection.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}