Design Undo/Redo for Video Editor using design pattern

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-02-19

Problem Statement

Design Undo/Redo functionality for a Video Editor application. Users should be able to perform actions (cut, trim, add effects, insert clips) and revert or reapply actions efficiently. The focus is on object-oriented design, modularity, and use of design patterns (Command pattern, Memento pattern, or similar).


✅ Functional Requirements


1.Video Editing Actions
•Actions include: cut, trim, add effect, insert clip, delete clip, change playback speed.
•Each action modifies the video timeline or clips.
2.Undo/Redo Operations
•Undo reverts the last performed action.
•Redo reapplies an undone action.
•Multiple levels of undo/redo must be supported.
3.History Management
•Maintain a stack of executed actions for undo.
•Maintain a stack of undone actions for redo.
•Clear redo stack when a new action is performed after undo.
4.Optional Features
•Group multiple actions as a single composite action (e.g., batch edit).
•Support saving snapshots of video state for complex undo operations.


🔐 Constraints & Assumptions


•Focus is on LLD, design patterns, and modularity, not video encoding or rendering.
•Actions should be reversible.
•System should efficiently handle long history of actions without excessive memory usage.


🧱 Key Components & Classes


1.Command Interface / Abstract Class
1 2class ICommand { 3public: 4 virtual void execute() = 0; 5 virtual void undo() = 0; 6};
1.Concrete Commands (Examples)
1 2class CutCommand : public ICommand { 3private: 4 Video* video; 5 int startTime, endTime; 6 Clip backupClip; // stores original clip for undo 7public: 8 void execute(); 9 void undo(); 10}; 11 12class AddEffectCommand : public ICommand { 13private: 14 Video* video; 15 string effectName; 16public: 17 void execute(); 18 void undo(); 19};
1.Invoker / ActionManager
1 2class ActionManager { 3private: 4 stack<ICommand*> undoStack; 5 stack<ICommand*> redoStack; 6public: 7 void performAction(ICommand* command); 8 void undo(); 9 void redo(); 10};
1.Video / Timeline Class
•Represents video clips, timeline, and state changes.
1 2class Video { 3private: 4 vector<Clip> clips; 5public: 6 void addClip(Clip clip); 7 void removeClip(int clipId); 8 void applyEffect(int clipId, string effect); 9};
1.Optional: Memento for Snapshots
•Stores video state for complex undo scenarios.


🔄 Example Workflow


1.User performs a cut action: CutCommand.execute().
2.ActionManager pushes the command onto the undoStack.
3.User performs add effect: AddEffectCommand.execute().
4.User clicks undo → undoStack.top().undo() → command moved to redoStack.
5.User clicks redo → redoStack.top().execute() → command moved back to undoStack.


🧪 Expected Deliverables


•Class diagram showing ICommand, concrete commands, ActionManager, and Video.
•Implementation of execute, undo, redo operations.
•Test cases:
•Single action undo/redo
•Multiple actions in sequence
•Undo after redo
•Composite actions (optional)


💡 Discussion Points


•Using Command pattern for encapsulating actions.
•Using Memento pattern for storing snapshots of video state.
•Memory trade-offs between full video snapshot vs minimal state for undo.
•Extensibility for new action types without changing core system.


Solution

Comments