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
Post a Comment