2013-08-06 4 views
0

리팩터링을 살펴 보았습니다. 일부는 트랜잭션 코드로 상태 머신 패턴을 적용 할 수있는 모듈이어야합니다. public class <StateName> extends State implements IExecutableState { ... } : 같을 것이다Java - Spring Framework - 상태 머신

public interface IExecutableState { 
    public void execute(); 
} 

public class StateMachine { 
    // final: 
    private static final Logger LOGGER = Logger.getLogger(StateMachine.class); 

    // instance private: 
    private HashMap<String, State> validStates; 
    private State currentState; 

    // getters: 
    public HashMap<String, State> getValidStates() { return this.validStates; } 
    public State getCurrentState() { return this.currentState; } 

    // setters: 
    public void setValidStates(HashMap<String, State> validStates) { 
     // assign the stateMachine attribute to each State from this setter, so that Spring doesn't enter an infinite loop while constructing a Prototype scoped StateMachine. 
     for (State s : validStates.values()) { 
      // validate that the State is defined with implements IExecutableState 
      if (!(s instanceof IExecutableState)) { 
       LOGGER.error("State: " + s.toString() + " does not implement IExecutableState."); 
       throw new RuntimeException("State: " + s.toString() + " does not implement IExecutableState."); 
      } 
      LOGGER.trace("Setting stateMachine on State: " + s.toString() + " to: " + this.toString() + "."); 
      s.setStateMachine(this); 
     } 
     LOGGER.trace("Setting validStates: " + validStates.toString() + "."); 
     this.validStates = validStates; 
    } 
    public void setCurrentState(State currentState) { 
     if (!(currentState instanceof IExecutableState)) { 
      LOGGER.error("State: " + currentState.toString() + " does not implement IExecutableState."); 
      throw new RuntimeException("State: " + currentState.toString() + " does not implement IExecutableState."); 
     } 
     LOGGER.trace("Setting currentState to: " + currentState.toString() + "."); 
     this.currentState = currentState; 
    } 
} 

public class State { 
    private StateMachine stateMachine; 
    public StateMachine getStateMachine() { return this.stateMachine; } 
    public void setStateMacine(StateMachine stateMachine) { this.stateMachine = stateMachine; } 
} 

모든 상태 : 처음에, 나는 다음과 같은 클래스를 만들었습니다.

그런 다음,이 모델에, 나는 상태 기의 인스턴스를 프로토 타입 범위 스프링 빈을 만들고, 모든 상태를 할당하고 싶었다.

그러나, 봄 WebFlow에서 사용할 수있는 몇 가지보고 후, 나는 완벽하게, 내가 찾는 상태 머신의 동작을 에뮬레이션 WebFlows를 참조하기 시작했다. 그리고 각 상태 흐름을 시각적으로 표현할 수있는 방식으로 XML에서 만듭니다.

내가 찾는거야 유일한 문제는 WebFlows가 웹 프로젝트/웹 응용 프로그램/(당신이 그들을 분류 할 중) 웹 사이트에 대한 의미입니다. 스프링 웹 플로우에서 얻을 수있는 <view-state> 태그와 매우 유사한 것을 찾고 있는데, 스프링 코어, 스프링 통합 또는 스프링 배치 프로젝트에서 실행중인 응용 프로그램에 대한 것입니다.

+0

의도적 확장 상태의 집합을 유지하고 싶은거야 좀 걸릴 수 있습니다, 또는 컴파일 타임에 폐쇄? – chrylis

+0

@chrylis : 내가 왜 scope = prototype을 사용했는지 물어 본다면, Thread 당 하나의 StateMachine 인스턴스를 실행할 수있는 데몬에 StateMachine 객체의 인스턴스가 여러 개 있기를 원합니다. 각 StateMachine이 초기 상태 (일종의 dequeueState 또는 waitUntilEventOccursState)에서 시작한 다음 결과 트랜잭션에 대해 조치를 취하여 해당 트랜잭션의 각 상태를 전파하고 완료되면 종료합니다. Spring WebFlows가 소개하는 개념은 내가 원하는 동작을 반영하고 도움을주는 플러그인을 가지고 있지만 WebFlow를위한 것입니다. – anonymous

+0

내가 물어 보는 이유는 컴파일 타임에 알려진 상태의 상태 머신이'Map '대신에'enum'으로 훨씬 더 깔끔하고, 쉽고, 효율적으로 구현 될 수 있으며, 기계 프레임 워크. – chrylis

답변