java - Implementing State Pattern -


i implementing state pattern in java application , need few clarifications.

the state machine has 5 states state 1 state 5. total of 5 events(event1 event5) causes state transition. not events applicable in states. if event not applicable in particular state application throw exception.

when state machine gets initialized starts state1.

following interface , context class.

/*  interface defining possible events in each state.  each implementer handle event in different manner.  */ public interface state {  /*   handlers each event. each implementer handle vent in different manner.  */  public void handleevent1(statecontext context);  public void handleevent2(statecontext context);  public void handleevent3(statecontext context);  public void handleevent4(statecontext context);  public void handleevent5(statecontext context);  // method enter state , action.  public void enter(statecontext context);  // method exit state , clean-up activity on exit .  public void exit(statecontext context); }  /*   context class handle state change , delegate event appropriate event handler of current state */ class statecontext {     private final reentrantreadwritelock lock = new reentrantreadwritelock();     private state currentstate = null;     statecontext() {         currentstate = new state1();    }     //handle event1 , pass appropriate event handler current state.    public void handleevent1() {       currentstate.handleevent1();     }        .        .        .    //handle event5 , pass appropriate event handler current state.    public void handleevent5() {       currentstate.handleevent5();     }     // method change state.     // method called each state when needs transit new state.    public void changestate(state newstate) {           accquirelock();           currentstate.exit();           currentstate = newstate;           currentstate.enter();               }     // release read lock , accquire write lock    public void accquirelock() {         lock.readlock().unlock()         lock.writelock().lock();    }     // accquire readlock , release write lock    public void releaselock() {         lock.readlock().lock()         lock.writelock().unlock();    } } 

to make simple, have provided implementation 1 state.

public class state1 implements state {        public void handleevent1(statecontext context) {           //hand1e event 1        }               .               .               .       public void handleevent5(statecontext context) {           //handle event 5        }          public void enter(statecontext context) {            //release lock here            context.releaselock();            /*here question.  java practice expose accquire , release lock in context object. , use exposed method here release lock.             */             // action on entering state. may take few seconds finish         }   } 

i want release lock after entering state. don't want hold lock till enter() finishes. if hold lock till enter finishes cannot handle other events , may timed-out.for events (which don't change state) need read state , based on state can process them or ignore them. if don't release lock cannot processed.also in other cases if event comes shutdown(this event changes state) state machine while enter() in progress cannot handle it. have shutdown state machine since not appropriate in continuing enter() after shutdown event has come.

my question: java programming practice expose accquirelock , releaselock api in context class , use them in each state class.

thanks, arun

to answer question, if lock held state "manager" class, class should 1 control lock access, not of actual state classes.

with regards statement holding lock until enter finishes, precisely point of lock: don't want other methods getting involved or interrupting. instead, should develop sort of event queue catch events , wait distribute them if receiving object busy. either that, or make specific exception whichever events know going interrupt, can bypass lock when specified event(s) fired.

if choose use method of interrupting, you're going have implement number of checks in each state class's enter method check if shutdown event has been triggered. other way see working make each state extend thread may interrupt/stop @ will, though not sound valid option in instance.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -