c# - Implementing undo/redo -
in drawing application, i'm implementing multi-level undo/redo using memento pattern. 1 problem facing memento (the state object) become large if drawing contains 1 or more image objects (my drawing object can have base64-encoded image objects embedded in it, visual studio's resx files), make undo/redo stacks climb several megabytes because of frequent mouse operations common in drawing apps. solve problem, introduced gzip compression saved zipped version of memento onto stack. resulted in 90% reduction in overall stack size.
now has resulted in problem. time takes zip / unzip memento has introduced considerable jags in object positioning/resizing.
one way solve use command pattern, i'm reluctant go path becuz introduce whole lot of work in many parts of application. other alternates see?
the solution can think of without switching entirely command pattern generate kind of incremental backup of object memento instead of using whole object.
the idea strip memento data haven't changed. store difference.
when doing undo, use current state of object , difference generate memento , inject in process.
here's how like:
- the caretaker going originator, wants able undo change.
- the caretaker first asks originator memento object.
- then whatever operation (or sequence of operations) going do.
- a compressor compare memento , new state of originator , generate diff object stored on operation stack
to undo:
- the compressor recreate memento object originator state , diff object
- the new memento used restore originator state.
the implementation of compressor class might tricky , depend on type of operations perform.
you using 1 single memento object , store diff objects can compressed if needed rather small.
Comments
Post a Comment