Created
September 8, 2009 05:44
-
-
Save jboner/182740 to your computer and use it in GitHub Desktop.
DI using var's and partial functions
This file contains 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
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