fork download
  1. interface InterfaceComponent {
  2. void doOperation();
  3. }
  4.  
  5. class MainComponent implements InterfaceComponent {
  6.  
  7. @Override
  8. public void doOperation() {
  9. System.out.print("World!");
  10. }
  11. }
  12.  
  13. abstract class Decorator implements InterfaceComponent {
  14. protected InterfaceComponent component;
  15.  
  16. public Decorator (InterfaceComponent c) {
  17. component = c;
  18. }
  19.  
  20. @Override
  21. public void doOperation() {
  22. component.doOperation();
  23. }
  24.  
  25. public void newOperation() {
  26. System.out.println("Do Nothing");
  27. }
  28. }
  29.  
  30. class DecoratorSpace extends Decorator{
  31.  
  32. public DecoratorSpace(InterfaceComponent c) {
  33. super(c);
  34. }
  35.  
  36. @Override
  37. public void doOperation() {
  38. System.out.print(" ");
  39. super.doOperation();
  40. }
  41.  
  42. @Override
  43. public void newOperation() {
  44. super.newOperation();
  45. System.out.println("New space operation");
  46. }
  47. }
  48.  
  49. class DecoratorComma extends Decorator {
  50.  
  51. public DecoratorComma(InterfaceComponent c) {
  52. super(c);
  53. }
  54.  
  55. @Override
  56. public void doOperation() {
  57. System.out.print(",");
  58. super.doOperation();
  59. }
  60.  
  61. @Override
  62. public void newOperation() {
  63. super.newOperation();
  64.  
  65. System.out.println("New comma operation");
  66. }
  67. }
  68.  
  69. class DecoratorHello extends Decorator {
  70.  
  71. public DecoratorHello(InterfaceComponent c) {
  72. super(c);
  73. }
  74.  
  75. @Override
  76. public void doOperation() {
  77. System.out.print("Hello");
  78. super.doOperation();
  79. }
  80.  
  81. @Override
  82. public void newOperation() {
  83. super.newOperation();
  84.  
  85. System.out.println("New hello operation");
  86. }
  87. }
  88.  
  89. class Main {
  90.  
  91. public static void main (String[] args) {
  92. Decorator c = new DecoratorHello(new DecoratorSpace(new DecoratorComma(new MainComponent())));
  93. c.doOperation(); // Результат выполнения программы "Hello, World!"
  94. c.newOperation(); // New hello operation
  95. }
  96. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Hello ,World!Do Nothing
New hello operation