Created
May 26, 2011 22:13
-
-
Save dholbrook/994216 to your computer and use it in GitHub Desktop.
service locator demo
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 ServiceLocatorTest extends App { | |
trait SimpleServiceLocator { | |
import scala.collection.immutable.HashMap | |
private var registry = new HashMap[String,Any]() | |
def instance[T](implicit manifest: Manifest[T]): Option[T] = { | |
for { | |
factory <- registry.get(manifest.toString) | |
instance <- Some(factory.asInstanceOf[() => T]()) | |
} yield instance | |
} | |
protected def register[T](factory: => T)(implicit manifest : Manifest[T]) { | |
registry += (manifest.toString -> factory _) | |
} | |
} | |
class EchoService { | |
def echo(echo: String): String = { | |
echo | |
} | |
} | |
trait EchoClient { | |
def callEcho(echoMe: String) | |
} | |
//Constructor Style | |
class EchoClientConstructor(echoService: EchoService) extends EchoClient { | |
def callEcho(echoMe: String){ | |
println(echoService.echo(echoMe + " Constructor Style")) | |
} | |
} | |
//Property Style | |
class EchoClientProperty() extends EchoClient { | |
var echoService: EchoService = null | |
def callEcho(echoMe: String){ | |
println(echoService.echo(echoMe + " Property Style")) | |
} | |
} | |
//Service Locator Style | |
class EchoClientServiceLocator() extends EchoClient { | |
def echoService = EchoModule.instance[EchoService].get | |
def callEcho(echoMe: String){ | |
println(echoService.echo(echoMe + " Service Locator Style" )) | |
} | |
} | |
//Def style | |
abstract class EchoClientDef() extends EchoClient { | |
def echoService: EchoService | |
def callEcho(echoMe: String){ | |
println(echoService.echo(echoMe + " Def Style")) | |
} | |
} | |
object EchoModule extends SimpleServiceLocator { | |
register[EchoService] { | |
new EchoService | |
} | |
register[EchoClientConstructor] { | |
new EchoClientConstructor(instance[EchoService].get) | |
} | |
register[EchoClientProperty] { | |
val client = new EchoClientProperty() | |
client.echoService = instance[EchoService].get | |
client | |
} | |
register[EchoClientServiceLocator] { | |
new EchoClientServiceLocator | |
} | |
register[EchoClientDef] { | |
new EchoClientDef { | |
def echoService = instance[EchoService].get | |
} | |
} | |
} | |
val echoClientA: EchoClient = EchoModule.instance[EchoClientConstructor].get | |
val echoClientB: EchoClient = EchoModule.instance[EchoClientProperty].get | |
val echoClientC: EchoClient = EchoModule.instance[EchoClientServiceLocator].get | |
val echoClientD: EchoClient = EchoModule.instance[EchoClientDef].get | |
echoClientA.callEcho("echo me") | |
echoClientB.callEcho("echo me") | |
echoClientC.callEcho("echo me") | |
echoClientD.callEcho("echo me") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment