You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Invoker.java 931B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package controller;
  7. import java.util.HashMap;
  8. import java.util.Stack;
  9. /**
  10. *
  11. * @author jechowma68968
  12. */
  13. public class Invoker
  14. {
  15. private HashMap<Object, Interface> commands;
  16. private Stack commandHistory;
  17. public Invoker()
  18. {
  19. commands = new HashMap<>();
  20. commandHistory = new Stack();
  21. }
  22. /**
  23. *
  24. * @param key ist das Object
  25. * @param value ist auch etwas
  26. */
  27. public void addCommand(Object key, Interface value){
  28. commands.put(key, value);
  29. }
  30. public void executeCommand(Object key){
  31. commandHistory.push(key);
  32. commands.get(key).execute();
  33. }
  34. public void undo(Object key){
  35. if (commandHistory.isEmpty()){
  36. return;
  37. }
  38. commands.get(commandHistory.pop()).undo();
  39. }
  40. }