2

I need some useful suggestion on design pattern for the following problem in Java.

I have three classes:

class A extends X implement Y {
    doA()
}
class B extends X implement Y {
    doB()
}
class C extends X implement Y {
    doC()
}

Now I would like create a class D that should reuse methods doA, doB and doC of classes A, B and C respectively.

Can anyone suggest a design for the above problem?

Nathaniel Jones
  • 939
  • 1
  • 14
  • 25
  • 5
    Is it important to your problem that `A B C` extends `X` and implements `Y` ? – gontard Sep 25 '14 at 12:36
  • 4
    What exactly are doA, doB and doC... are they related to the X base class or the Y interface? – Jason Down Sep 25 '14 at 12:38
  • Its required to extend class X because class X will have some common functionalities. The doA, doB and doc are having specific functionalities with respect to the classes A, B & C. I would like to reuse those doA, doB and doC methods in class D in some way. – D Lakshmipathi Sep 26 '14 at 05:43

5 Answers5

4

I think you need to use paradigm "Prefer composition over inheritance". Design class D that includes instances of A, B, C and call their methods as necessary. In addition, D can implement Y if necessary and delegate corresponding API calls to either A or B or C.

Aivean
  • 10,692
  • 25
  • 39
1

This isn't directly possible as it stands.

Your best bet, if your design will allow, is to recreate X as an interface, and the others as interfaces that extend X. That will allow D to implement all the others.

Failing that, you might (depending on what you're trying to do) get away with having D hold a private instance of A, B and C, and then get it to delegate the methods you mention to those instances. But bear in mind that you'll have three different X instances if you do it like that.

The right answer is probably that your class hierarchy is already not quite what it should be! But there's not enough detail in the question to be certain.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
1

You can try something like this:

class A extends X implement Y{
  doA()
}
class B extends X implement Y{
  doB()
}
class C extends X implement Y{
   doC()
}

interface Delegator { 
    doSomething();
}

class DBuider {

    public DBuider with(Delegator d) {
        // create chain and store it
        return this;
    }

    Chain build() {
        // return new D with resulted chain
    }

    class Chain {
        Delegator del;
        Chain next;
    }
}

class D implements Y {
    Chain c;

    static DBuilder create() {
        return new DBuilder();
    }

    doD() {
        Chain t = c;
        while (t != null) {
            t.del.doSomething();
            t = t.next;
        }
    }
}

----- Usage -----

D.create().with(() -> new A().doA()).with(() -> new B().doB()).build().doD();
pbespechnyi
  • 2,251
  • 1
  • 19
  • 29
0

You could compose your new class D from your existing classes:

class D {
    private A a = new A();
    private B b = new B();
    private C c = new C();

    public void foo() {
        this.a.doA();
        this.b.doB();
        this.c.doC();
    }
}
ifloop
  • 8,079
  • 2
  • 26
  • 35
0

Without knowing all the details, this feels like a composition problem.

Class D
{
   private A _a = new A();
   private B _b = new B();
   private C _c = new C();

   doA()
   {
      _a.doA();
   }

   doB()
   {
     _b.doB();
   }

   doC()
   {
     _c.doC();
   }
}

This may or may not actually be appropriate based on what you are trying to do.

Matthew Vines
  • 27,253
  • 7
  • 76
  • 97