State Pattern with Aspect
Today I am going discuss Implementation of State Pattern throgh AspectJ
In state Pattern there are two roles to watch out for, The Context and the state.
The Context Role contains state.
Now when an operation is required to be carried out on context then the state is checked
Now based on the state, the operation on the state is undertaken and if required the new state is set in context.
The mentioned operation is carried out through the AspectJ.
The context has the state within itself and the State role has the perform method for performing operations.
They are InitialState, AckState and ClosedState.
Now four pointcuts are used here i)interceptInitialState --> To catch initialization of MyConnecton class.
ii) inceptOtherState --> To incept the invocation of any method of MyConnection class starting with getConn.
iii) interceptClosedState --> To incept the invocation of closed method of MyConnection class. iv) doNotInceptState --> Reverse of interceptClosedState pointcut. The After advice which is applied on "interceptInitialState" pointcut sets the state of the context to InitialState and performs the action accordingly.
The around advice which is applied on inceptOtherState and doNotInceptState pointcut checks the state and perform operation on the State accordingly.
Similarly the before advice, which acts on interceptClosedState pointcut does the same thing.
This is short about implementation of State Pattern though AspectJ.
Really Interesting Huh!!!!!
In my next post, I will try to provide some insight about Memento Pattern.Believe me its is really nice...
Please do not forget to post your queries and suggestions, which is really appreciated
Till then Happy Coding :-)
In state Pattern there are two roles to watch out for, The Context and the state.
The Context Role contains state.
Now when an operation is required to be carried out on context then the state is checked
Now based on the state, the operation on the state is undertaken and if required the new state is set in context.
The mentioned operation is carried out through the AspectJ.
public abstract aspect MyNewAspectStatePattern { public interface Context { public void setState(State state); public State getState(); } public interface State { public void perform(); } public abstract pointcut interceptInitialState(Context context/*,com.aspectj.target.State state*/); }In the above aspect there are two states as mentioned, the Context and the State.
The context has the state within itself and the State role has the perform method for performing operations.
public aspect MyNewAspectStatePatternConc extends MyNewAspectStatePattern { declare parents:com.aspectj.target.MyConnection implements Context; declare parents:com.aspectj.target.InitialState implements State; declare parents:com.aspectj.target.AckState implements State; declare parents:com.aspectj.target.ClosedState implements State; public State com.aspectj.target.MyConnection.state; public void com.aspectj.target.MyConnection.setState(State state) { //this.setState(state); this.state=state; } public State com.aspectj.target.MyConnection.getState() { return state; } public void com.aspectj.target.InitialState.perform() { System.out.println(" Work Performed by Initial State "); } public void com.aspectj.target.AckState.perform() { System.out.println(" Work Performed by Ack State "); } public void com.aspectj.target.ClosedState.perform() { System.out.println(" Work Performed by Closed State "); } public pointcut interceptInitialState(Context context/*,com.aspectj.target.State state*/):initialization( com.aspectj.target.MyConnection.new(..)) && this(context) /*&& args(..,state)*/; public pointcut inceptOtherState(Context context):execution(* com.aspectj.target.MyConnection.getConn*(..)) && this(context); public pointcut doNotInceptState(Context context):!execution(* com.aspectj.target.MyConnection.closed(..)) && this(context); public pointcut interceptClosedState(Context context):execution(* com.aspectj.target.MyConnection.closed(..)) && this(context); after(Context context/*,com.aspectj.target.State state*/):interceptInitialState(context/*,state*/) && !cflow(adviceexecution()) { State state = (State)new com.aspectj.target.InitialState(); context.setState(state); context.getState().perform(); } void around(Context context1,Context context2):inceptOtherState(context1) && doNotInceptState(context2) && !cflow(adviceexecution()) { if( ((context1).getState() instanceof com.aspectj.target.InitialState) || ((context1).getState() instanceof com.aspectj.target.AckState)) { context1.setState((State)new com.aspectj.target.AckState()); context1.getState().perform(); //proceed(context1,context2); } else { System.out.println(" Connection has not been Initialized or it is closed"); } } before(Context context):interceptClosedState(context) && !cflow(adviceexecution()) { if( ((context).getState() instanceof com.aspectj.target.InitialState) || ((context).getState() instanceof com.aspectj.target.AckState) ) { context.setState((State)new com.aspectj.target.ClosedState()); context.getState().perform(); } else { System.out.println(" Connection cannot be closed as it is neither initialized nor Ack "); } } }In the above aspect , Context Role is implemented through MyConnection and three States are used.
They are InitialState, AckState and ClosedState.
Now four pointcuts are used here i)interceptInitialState --> To catch initialization of MyConnecton class.
ii) inceptOtherState --> To incept the invocation of any method of MyConnection class starting with getConn.
iii) interceptClosedState --> To incept the invocation of closed method of MyConnection class. iv) doNotInceptState --> Reverse of interceptClosedState pointcut. The After advice which is applied on "interceptInitialState" pointcut sets the state of the context to InitialState and performs the action accordingly.
The around advice which is applied on inceptOtherState and doNotInceptState pointcut checks the state and perform operation on the State accordingly.
Similarly the before advice, which acts on interceptClosedState pointcut does the same thing.
This is short about implementation of State Pattern though AspectJ.
Really Interesting Huh!!!!!
In my next post, I will try to provide some insight about Memento Pattern.Believe me its is really nice...
Please do not forget to post your queries and suggestions, which is really appreciated
Till then Happy Coding :-)
Comments
Post a Comment