Dynamically changing the behavior of an object via composition (the State design pattern) : State Pattern « Design Pattern « Java






Dynamically changing the behavior of an object via composition (the State design pattern)

Dynamically changing the behavior of an object via composition (the State design pattern)

// : c07:Transmogrify.java
// Dynamically changing the behavior of an object via composition (the State design pattern).
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

abstract class Actor {
  public abstract void act();
}

class HappyActor extends Actor {
  public void act() {
    System.out.println("HappyActor");
  }
}

class SadActor extends Actor {
  public void act() {
    System.out.println("SadActor");
  }
}

class Stage {
  private Actor actor = new HappyActor();

  public void change() {
    actor = new SadActor();
  }

  public void performPlay() {
    actor.act();
  }
}

public class Transmogrify {
  public static void main(String[] args) {
    Stage stage = new Stage();
    stage.performPlay();
    stage.change();
    stage.performPlay();
  }
} ///:~


           
       








Related examples in the same category

1.State Pattern in Java
2.State pattern in Java 2State pattern in Java 2