Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Last active August 29, 2015 13:58
Show Gist options
  • Save Shinpeim/10393206 to your computer and use it in GitHub Desktop.
Save Shinpeim/10393206 to your computer and use it in GitHub Desktop.
Decorator パターンの例
object DecoratorSample extends App{
val nyan: Nyan = new NyanImpl
nyan.nyan("にゃーん") // => にゃーん
val nyanWithDecorator: Nyan = new NyanDecorator(new NyanImpl)
nyanWithDecorator.nyan("にゃーん") // => にゃーんなのです
// という感じで、既存のクラス(NyanImpl)を書き換えずに、
// その動きを変える(拡張する)ことができるのが
// デコレーターパターン
}
trait Nyan {
def nyan(arg: String): Unit
}
class NyanImpl extends Nyan {
def nyan(arg: String) = {
// process something
println(arg)
}
}
class NyanDecorator(decoratee: Nyan) extends Nyan {
def nyan(arg: String) = {
val decoratedArg = arg + "なのです"
decoratee.nyan(decoratedArg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment