design patterns - Implementing undo in Java for state changes -


i starting implementing command pattern in hope useful solution problem of providing undo operation. facing issue:

implementing undo when operations involved rather easy: when i've added 5 number subtract 5. when i've added object list, remove it, , on. if have total state , not list?

an example: model information thread in class:

public class threadinfo implements comparable<threadinfo> {      final int id;     string name;     int priority;     string state;     int waitcount;     // ... } 

certain information not change, instance id. undoing waitcount easy described above, subtract. priority or state? not clear how undo these information.

the idea came with: when initializing command object, preserve old state in it's object: passing relevant data constructor:

public mycommand(int priority, string state) {    previouspriority = priority;    previousstate = state; } 

or better let threadinfo have list of states , priorities being first elements current?

just hold old state in command. example

 class changefoo {     changefoo (bar bar, foo newfoo){}     void execute(){         oldfoo = bar.getfoo();         bar.setfoo(newfoo);     }     void undo(){         bar.setfoo(oldfoo);     }  } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -