Last active
December 26, 2015 04:39
-
-
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
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 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