fork download
  1. interface A {
  2. void doSomethingA1();
  3. void doSomethingA2();
  4. void doSomethingA3();
  5. }
  6.  
  7.  
  8. interface B {
  9. void doSomethingB();
  10. A getA();
  11. }
  12.  
  13.  
  14. interface C {
  15. void doSomethingC();
  16. A getA();
  17. }
  18.  
  19. interface X {
  20. A getA();
  21. B getB();
  22. C getC();
  23. void doSomethingX();
  24. }
  25.  
  26.  
  27. class AImpl implements A {
  28. int x;
  29. public void doSomethingA1() { /* ... */ }
  30. public void doSomethingA2() { /* ... */ }
  31. public void doSomethingA3() { /* ... */ }
  32. }
  33.  
  34.  
  35. class BImpl implements B {
  36. A a;
  37. public A getA() { return a; }
  38. public void doSomethingB() { /* ... */ }
  39. }
  40.  
  41. /* если нужно явно имплементировать А (без делегации)
  42. class BImpl implements B, A {
  43.   public A getA() { return this; }
  44.   public void doSomethingA1() { }
  45.   public void doSomethingA2() { }
  46.   public void doSomethingA3() { }
  47.   public void doSomethingB() { }
  48. }
  49. */
  50.  
  51. class CImpl implements C {
  52. A a;
  53. public A getA() { return a; }
  54. public void doSomethingC() { /* ... */ }
  55. }
  56.  
  57. class XImpl implements X {
  58. B b;
  59. C c;
  60.  
  61. public B getB() { return b; }
  62. public C getC() { return c; }
  63.  
  64. // нужно явно разрешить diamong problem
  65. public A getA() { return b.getA(); }
  66.  
  67. public void doSomethingX() { /* ... */ }
  68. }
  69.  
  70.  
  71. class Main {
  72. public static void main(String[] args) { }
  73. void usage() {
  74. X x = null; // createX(...)
  75. x.doSomethingX();
  76. x.getA().doSomethingA1();
  77. x.getA().doSomethingA2();
  78. x.getA().doSomethingA3();
  79. x.getB().doSomethingB();
  80. x.getC().doSomethingC();
  81. }
  82.  
  83. }
  84.  
Success #stdin #stdout 0.04s 4575232KB
stdin
Standard input is empty
stdout
Standard output is empty