Skip to content

Instantly share code, notes, and snippets.

@coderunner
Created August 12, 2011 02:21
Show Gist options
  • Select an option

  • Save coderunner/1141302 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/1141302 to your computer and use it in GitHub Desktop.
dependency examples
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