Created
August 12, 2011 02:21
-
-
Save coderunner/1141302 to your computer and use it in GitHub Desktop.
dependency examples
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
| public class ExampleClass | |
| { | |
| private final DependencyInterface dependency; | |
| //no dependency injection | |
| public ExampleClass() | |
| { | |
| //this makes the class hard to test | |
| this.dependency = new Dependency(); | |
| //... | |
| } | |
| //inject the concrete dependency | |
| public ExampleClass(Dependency dependency) | |
| { | |
| //this makes the class slightly more testable | |
| //if proper test tools are used because | |
| //the dependency can be setup or mocked | |
| this.dependency = dependency; | |
| //... | |
| } | |
| //inject the interface | |
| public ExampleClass(DependencyInterface dependency) | |
| { | |
| //this makes the class easily testable as a test | |
| //implementation can be supplied | |
| this.dependency = dependency; | |
| //... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment