|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package command;
-
- import command.CommandInterface;
- import java.util.HashMap;
- import java.util.Stack;
-
-
- /**
- *
- * @author PC
- */
- public class CommandInvoker {
- private HashMap<Object, CommandInterface> commands;
- private Stack<CommandInterface> undoStack;
-
- public CommandInvoker()
- {
- commands = new HashMap<>();
- undoStack = new Stack<>();
- }
-
- public void addCommand(Object key, CommandInterface value)
- {
- commands.put(key, value);
- }
-
- public void executeCommand(Object key)
- {
- CommandInterface cmd = commands.get(key);
- cmd.execute();
- if(cmd.isUndoable())
- {
- undoStack.push(cmd);
- }
- }
-
- public void undoCommand()
- {
- if(!undoStack.empty())
- {
- undoStack.pop().undo();
- }
- }
-
- }
|