title: Java: Strategy pattern with access to superclass' fields and methods?
body: <p>I have an abstract class <code>Parent</code> with abstract methods <code>foo()</code> and <code>bar()</code> and other non-abstract methods and fields. I need to create 4 subclasses (and later more) to cover each combination of different variations on <code>foo()</code> and <code>bar()</code>: <code>fooA()</code>, <code>fooB()</code>, <code>barA()</code>, <code>barB()</code>. These variations need access to other fields and methods of <code>Parent</code>.
  In other words, if Java supported multiple inheritance, then I would have something like:</p>
  
  <pre><code>abstract class Parent{
      abstract foo(){}
      abstract bar(){}
      //other fields and methods that will be accessed foo and bar are PROTECTED
  }
  
  abstract class FooA extends Parent{
      @Override
      foo(){ ... }
  }
  abstract class FooB extends Parent{
      @Override
      foo(){ ... }
  }
  abstract class BarA extends Parent{
      @Override
      bar(){ ... }
  }
  abstract class BarB extends Parent{
      @Override
      bar(){ ... }
  }
  
  class ChildAA extends FooA, BarA{   
  }
  
  class ChildAB extends FooA, BarB{
  }
  
  class ChildBA extends FooB, BarA{
  }
  
  class ChildBB extends FooB, BarB{
  }
  </code></pre>
  
  <p>I have found two solutions, each of which sort of works, but just about. Are there better ways to achieve this behaviour? My solutions are as follows: </p>
  
  <p>1) First solution:</p>
  
  <pre><code>abstract class Parent {
      foo(){ 
          /* behaves like fooA */
      }
      //other fields and methods that will be accessed foo and bar are PROTECTED
  }
  
  class ChildAA extends Parent{
      barA(){ ... }
  }
  
  class ChildAB extends Parent{
      barB(){ ... }
  }
  
  class ChildBA extends ChildAA{
      @Override
      foo(){ /* behaves like fooB */ }
  |
  
  class ChildBB extends ChildAB{
      @Override 
      foo(){ /* behaves like fooB */ }
  }
  </code></pre>
  
  <p>The problem with this is that it duplicates the code for <code>fooB()</code> and all additional methods that only <code>fooB()</code> needs. The problem gets exponentially worse when more variations are needed.</p>
  
  <p>2) After looking around I found the design pattern Strategy, which can be used to achieve the behaviour but is awkward because the variations need to access <code>Parent</code>'s fields and methods:</p>
  
  <pre><code>abstract class Parent{
      Fooable fooable;
      Barable barable;
      foo(){ fooable.foo(); }
      bar(){ barable.bar(); }
      //other fields and methods that will be accessed foo and bar are PUBLIC
  }
  
  abstract class ImplementableParent{
      Parent p;
      ImplementableParent(Parent p) { this.p = p; }
  }
  
  interface Fooable{
      foo();
  }
  class FooA extends ImplementableParent implements Fooable{
      FooA(Parent p){ super(p); }
      @Override 
      foo(){ /* behaves like FooA */ }
  }
  class FooB extends ImplementableParent implements Fooable{
      FooB(Parent p){ super(p); }
      @Override 
      foo(){ /* behaves like FooB */ }
  }
  
  interface Barable{
      bar();
  }
  class BarA extends ImplementableParent implements Barable{
      BarA(Parent p) { super(p); }
      @Override 
      bar() { /* behaves like BarA */ }
  }
  class BarB extends ImplementableParent implements Barable{
      BarB(Parent p) { super(p); }
      @Override 
      bar() { /* behaves like BarB */ }
  }
  
  class ChildAA extends Parent{
      fooable = new FooA(this);
      barable = new BarA(this);
  }
  
  class ChildAB extends Parent{
      fooable = new FooA(this);
      barable = new BarB(this);
  }
  
  class ChildBA extends Parent{
      fooable = new FooB(this);
      barable = new BarA(this);
  }
  
  class ChildBB extends Parent{
      fooable = new FooB(this);
      barable = new BarB(this);
  }
  </code></pre>
  
  <p>This gets rid of repetitions of variations and can be extended to accommodate more variations. However, now the fields and methods of <code>Parent</code> are public and the whole thing feels very convoluted. I am also concerned about performance overhead, since <code>FooA</code>, <code>FooB</code>, <code>BarA</code> and <code>BarB</code> access the <code>Parent</code> methods indirectly, although I haven't tested it. </p>
  
  <p>Is there a better way to achieve the behaviour?</p>