D Lakshmipathi
D Lakshmipathi

Reputation: 65

suggestion on design pattern in java

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?

Upvotes: 2

Views: 563

Answers (5)

pbespechnyi
pbespechnyi

Reputation: 2291

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();

Upvotes: 1

chiastic-security
chiastic-security

Reputation: 20520

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.

Upvotes: 1

Matthew Vines
Matthew Vines

Reputation: 27581

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.

Upvotes: 0

Aivean
Aivean

Reputation: 10882

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.

Upvotes: 4

ifloop
ifloop

Reputation: 8386

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();
    }
}

Upvotes: 0

Related Questions