State

Intent

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Class diagram of the original GoF pattern

structure-GoF:state

Improvement in MixJuice

Solved Problem(s)

Extensibility problem
It is impossible to add new operation to State without modifying the source code. (This problem is not mentioned in the GoF book.)

Used programming technique(s)

Abstract method addition to State.

Consequences

Structure and pseudo code

structure:state_sample1

module original {
  define class Context {
    State state;
    define void request(){
      state.handle();
    }
  }
  define abstract class State {
    define abstract void handle();
  }
  define class ConcreteStateA extends State {
    void handle(){...}
  }
  define class ConcreteStateB extends State {
    void handle(){...}
  }
}
module extension extends original {
  class State {
    define abstract void newOperation();
  }
  class ConcreteStateA {
    void newOperation(){...}
  }
  class ConcreteStateB {
    void newOperation(){...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top