Last active
August 29, 2015 13:58
-
-
Save Shinpeim/10393206 to your computer and use it in GitHub Desktop.
Decorator パターンの例
This file contains hidden or 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 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