Saturday, August 28, 2010

Design Patterns em Scala: State

Segue mais dois padrões de projeto em Scala:State:State.scalapackage scala.behavioral// Mudar o comportamento mude de acordo com seu estado// Abstract Implementationclass Context { var state:State = _ state = new NullState() def handle() { state.handle() }}trait State { def handle()}// Concrete Implementationclass NullState extends State { override def handle() { println("blank algorithm, maybe throw an exception of unitilized context") }}class StateA extends State { override def handle() { println("algorithm for state A") }}class StateB extends State { override def handle() { println("algorithm for state B") }}// Clientobject StateClient extends Application { var context = new Context() context.state = new StateA() context.handle() context.state = new StateB() context.handle()}

No comments:

Post a Comment