fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. WorkDistributer wd = new WorkDistributer();
  13. wd.enable();
  14. wd.performAction((w) -> {w.printHello();});
  15. wd.disable();
  16. wd.performAction((w) -> {w.printHello();});
  17. wd.enable();
  18. wd.performAction((w) -> {w.printAnswer();});
  19. wd.disable();
  20. wd.performAction((w) -> {w.printAnswer();});
  21. }
  22. }
  23. class WorkDistributer
  24. {
  25. private boolean enabled = false;
  26. private ActionPerformer worker;
  27. public WorkDistributer() {
  28. this.worker = new Worker();
  29. }
  30. public void enable() {
  31. enabled = true;
  32. }
  33. public void disable() {
  34. enabled = false;
  35. }
  36. public void performAction(ActionCommand command) {
  37. if(this.enabled) {
  38. command.run(this.worker);
  39. }
  40. }
  41. }
  42. class Worker implements ActionPerformer {
  43. public void printHello() {
  44. System.out.println("hello");
  45. }
  46. public void printAnswer() {
  47. System.out.println(21 * 2);
  48. }
  49. }
  50.  
  51. interface ActionPerformer {
  52. public void printHello();
  53. public void printAnswer();
  54. }
  55.  
  56. interface ActionCommand {
  57. public void run(ActionPerformer worker);
  58. }
Success #stdin #stdout 0.07s 33708KB
stdin
Standard input is empty
stdout
hello
42