Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. public interface Command() {
  2. public void execute();
  3. }
  4.  
  5. public class MyCommand implements Command {
  6. private Receiver receiver;
  7.  
  8. public MyCommand(Receiver receiver) {
  9. this .receiver = receiver;
  10. }
  11.  
  12. @Override
  13. public void execute() {
  14. receiver.action();
  15. }
  16. }
  17.  
  18. public class Receiver {
  19. public void action() {
  20. System.out.println("command received!")
  21. }
  22. }
  23.  
  24. public class Invoker {
  25. private Command command;
  26.  
  27. public Invoker(Command command) {
  28. this. command = command;
  29. }
  30.  
  31. public void action() {
  32. command.execute();
  33. }
  34. }
  35.  
  36. public class Test {
  37. public static void main(String[] args) {
  38. Receiver receiver = new Receiver();
  39. Command cmd = new MyCommand(receiver);
  40. Invoker invoker = new Invoker(cmd);
  41. invoker.action();
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement