Skip to content

Instantly share code, notes, and snippets.

@danclien
Last active December 26, 2015 04:39
Show Gist options
  • Save danclien/7095296 to your computer and use it in GitHub Desktop.
Save danclien/7095296 to your computer and use it in GitHub Desktop.
Port of "Hello StructureMap" in C# over to the Cake Pattern in Scala. From https://gist.github.com/joshuaflanagan/397213
object HelloCakePattern {
Container.appEngine.run
object Container
extends AppEngineComponent
with EnglishGreeterComponent
with ConsoleOutputDisplayComponent {
val appEngine = new AppEngine
val greeter = new EnglishGreeter
val outputDisplay = new ConsoleOutputDisplay
}
trait AppEngineComponent extends IAppEnginerComponent {
this: IGreeterComponent with IOutputDisplayComponent =>
class AppEngine extends IAppEngine {
def run = outputDisplay.show(greeter.getGreeting)
}
}
trait IAppEnginerComponent {
val appEngine: IAppEngine
trait IAppEngine {
def run: Unit
}
}
trait IGreeterComponent {
val greeter: IGreeter
trait IGreeter {
def getGreeting: String
}
}
trait EnglishGreeterComponent extends IGreeterComponent {
class EnglishGreeter extends IGreeter {
def getGreeting: String = "Hello"
}
}
trait FrenchGreeterComponent extends IGreeterComponent {
class FrnechGreeter extends IGreeter {
def getGreeting: String = "Bonjour"
}
}
trait IOutputDisplayComponent {
val outputDisplay: IOutputDisplay
trait IOutputDisplay {
def show(message: String): Unit
}
}
trait ConsoleOutputDisplayComponent extends IOutputDisplayComponent {
class ConsoleOutputDisplay extends IOutputDisplay {
def show(message: String): Unit = println(message)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment