Created
August 12, 2011 03:01
-
-
Save coderunner/1141345 to your computer and use it in GitHub Desktop.
manual vs di
This file contains hidden or 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
| import org.inject.Injector; | |
| public class MainClass | |
| { | |
| private Injector injector; | |
| /** MANUAL OBJECT TREE SETUP **/ | |
| public void manualObjectTreeSetup() | |
| { | |
| //create the dependency tree | |
| //this can get really annoying | |
| AnotherDependencyInterface anotherDependency = | |
| new AnotherDependency(); | |
| DependencyInterface dependency = | |
| new Dependency(anotherDependency); | |
| //create the object | |
| ExampleClass object = new ExampleClass(dependency); | |
| } | |
| /** USING DEPENDENCY INJECTION **/ | |
| public void initInject() | |
| { | |
| //setup injection bindings, this is done once at initialization | |
| Injector.Builder builder = new Injector.Builder(); | |
| injector = builder | |
| .addClassBinding(AnotherDependencyInterface.class, AnotherDependency.class) | |
| .addClassBinding(DependencyInterface.class, Dependency.class) | |
| .addClassBinding(ExampleClass.class, ExampleClass.class) | |
| .build(); | |
| } | |
| public void injectObjectTreeSetup() | |
| { | |
| //create the required object | |
| //the injector will create the require dependencies and inject | |
| //them in the newly create instance | |
| ExampleClass object = injector.createObject(ExampleClass.class); | |
| } | |
| /** MAIN **/ | |
| public static void main(String args[]) | |
| { | |
| MainClass main = new MainClass(); | |
| //manual setup | |
| main.manualObjectTreeSetup(); | |
| //using DI library | |
| main.initInject(); | |
| main.injectObjectTreeSetup(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment