Memento Pattern with AspectJ
Hello Friends, This time i have implemented Memento Pattern with AspectJ.
In Memento Pattern, there are two roles: 1) Originator & 2)Memento
Originator is the Object or the Entity that we are working with.
Memento is the artifact where we are going to save the state of the Originator, so
that when required we can roll back the current state of the Originator with the state saved in the Memento.
Here I have saved only the state of the Originator during Initialization. Multiple states
of Originator as required can be saved in Memento and can be restored when required based on the requirement.
Intersting Huh!!!! We are going to implement this feature with AspectJ
The Originator Role has got two Methods:
a) setMemento --> To set the current state in Memento
b) restoreState --> used in restoring state
The Memento role has got two methods:
a)setState --> To set the current state of the Originator Type in Memento
b)getState --> To get the state of the Originator Type from Memento
There is One abstract pointCut inceptMemento, which will be implemented in the implemented
in the concrete implementation of "MyNewAspectMementoPattern"
The Originator restoreState() actually fetches the Memento registered with that Particular Originator and
calls its getState().
Similary the setMemento() actually calls the setState() of the Memento.
The setMemento() is called during initialization of any Originator Type which is
captured by the inceptMemento pointcut and the Memento that will be associated with
a particular Originator Type is being maintained in the after advice of
inceptMemento pointcut, through a WeakHashMap.
This is Short about Memento Pattern through AspectJ.
I will wrap up my AspectK edition out here only.
But discussions will Open and I will look forward to it.
In my next Blog, i will try to provide some insight regarding Spring3 JTA Transacations.
I have used a XA-DataSource and the underlying JTA implementation is Atomikos.
Though there are many articles are Available on the above mentioned issues to be discussed, but
during configuration I faced a lot of Issue, Which i will try to share with all of You.
Till then Happy Coding, and Be safe.... :-)
In Memento Pattern, there are two roles: 1) Originator & 2)Memento
Originator is the Object or the Entity that we are working with.
Memento is the artifact where we are going to save the state of the Originator, so
that when required we can roll back the current state of the Originator with the state saved in the Memento.
Here I have saved only the state of the Originator during Initialization. Multiple states
of Originator as required can be saved in Memento and can be restored when required based on the requirement.
Intersting Huh!!!! We are going to implement this feature with AspectJ
public abstract aspect MyNewAspectMementoPattern { public interface Originator { public void setMemento(Memento memento); //public Originator getObject(); public Originator restoreState(); } public interface Memento { public void setState(Originator originator); public Originator getState(); } public abstract pointcut inceptMemento(Originator employee,Memento memento); }In "MyNewAspectMementoPattern" aspect I have defined two roles as defined above.
The Originator Role has got two Methods:
a) setMemento --> To set the current state in Memento
b) restoreState --> used in restoring state
The Memento role has got two methods:
a)setState --> To set the current state of the Originator Type in Memento
b)getState --> To get the state of the Originator Type from Memento
There is One abstract pointCut inceptMemento, which will be implemented in the implemented
in the concrete implementation of "MyNewAspectMementoPattern"
public aspect MyNewAspectMementoPatternConc extends MyNewAspectMementoPattern{ declare parents : com.aspectj.target.Employee implements Originator; declare parents : com.aspectj.target.DefaultMemento implements Memento; /*public com.aspectj.target.Employee com.aspectj.target.Employee.getObject() { return new com.aspectj.target.Employee(this.getId(),this.getName()); }*/ public Originator com.aspectj.target.Employee.restoreState() { Memento memento = originatorMementoMapping.get(this); if(memento != null) return memento.getState(); else return null; } public void com.aspectj.target.Employee.setMemento(Memento memento) { memento.setState((Originator)this); } public void com.aspectj.target.DefaultMemento.setState(Originator originator) { this.setEmployee((com.aspectj.target.Employee)originator); } public Originator com.aspectj.target.DefaultMemento.getState() { Originator originator = null; if(this.getEmployee() != null) { originator = (Originator)(new com.aspectj.target.EmployeeSo, from the Above Concrete implementation of Aspect we can infer that:
(this.getEmployee().getId(),this.getEmployee().getName())); this.setEmployee(null); } return originator; } Memento memento; static WeakHashMaporiginatorMementoMapping = new WeakHashMap (); public pointcut inceptMemento(Originator employee,Memento memento):
initialization( com.aspectj.target.Employee.new(..)) && this(employee) && args(..,memento); after(Originator employee,Memento memento):inceptMemento(employee,memento) && !cflow(adviceexecution()) { /*System.out.println(" Object Created --> "); Object[] args = thisJoinPoint.getArgs(); for(Object obj:args) { System.out.println("Type:"+obj.getClass().getName()+":"+obj.toString()); }*/ Memento defaultMemento = (Memento)new com.aspectj.target.DefaultMemento(); originatorMementoMapping.put(employee, defaultMemento); System.out.println(employee.getClass().getName()+" class with"+
defaultMemento.getClass().getName() +" has been created successfully"); System.out.println("**** Saving Initial State of "+employee.getClass().getName()+" ****"); employee.setMemento(defaultMemento); System.out.println(" *** Saving state is successful *** "); } }
The Originator restoreState() actually fetches the Memento registered with that Particular Originator and
calls its getState().
Similary the setMemento() actually calls the setState() of the Memento.
The setMemento() is called during initialization of any Originator Type which is
captured by the inceptMemento pointcut and the Memento that will be associated with
a particular Originator Type is being maintained in the after advice of
inceptMemento pointcut, through a WeakHashMap.
This is Short about Memento Pattern through AspectJ.
I will wrap up my AspectK edition out here only.
But discussions will Open and I will look forward to it.
In my next Blog, i will try to provide some insight regarding Spring3 JTA Transacations.
I have used a XA-DataSource and the underlying JTA implementation is Atomikos.
Though there are many articles are Available on the above mentioned issues to be discussed, but
during configuration I faced a lot of Issue, Which i will try to share with all of You.
Till then Happy Coding, and Be safe.... :-)
Comments
Post a Comment