Skip to content

Instantly share code, notes, and snippets.

@jboner
Created September 8, 2009 05:44
Show Gist options
  • Save jboner/182740 to your computer and use it in GitHub Desktop.
Save jboner/182740 to your computer and use it in GitHub Desktop.
DI using var's and partial functions
object HttpBuilder {
var buildHttp: () => HttpThingIntf = new NormalHttpThing
}
// I want to change what is built by the HttpBuilder:
HttpBuilder.buildHttp = () => new MockHttpThing
// And to build a new HttpThing:
val myHttpThing = HttpBuilder.buildHttp()
// You can extend the paradigm with PartialFunctions:
object HttpBuilder {
var buildHttp: PartialFunction[Unit, HttpThingIntf] = {
case _ => new NormalHttpThing
}
}
// And you can modify the functionality with a conditional:
HttpBuilder.buildHttp = {
case _ if someCondition => new Condition1HttpMock
case _ if someOtherCondition => new Condition2HttpMock
} orElse HttpBuilder.buildHttp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment