mercredi 9 août 2017

One implementation calling the other implementations of the interface

I have an interface which is already implemented by two classes and I am adding a third class. The new implementation I am adding calls one of the existing two implementations depending on some condition. Is this a bad practice as per interface implementations?

public interface iface {
         public void method();
}


public class A implements iface {
        public void method() {
        //
        }
}

public class B implements iface {
        public void method() {
        //
        }
}

public class C implements iface {
        public void method() {
               //some logic 
               if(this) call a.method();
               else call b.method();
        }
}

Here caller needs to call C's implementation in some of the cases. I don't want to create wrapper over A & B because we don't always want to go through C. Is there any other way I should have this rather than one implementation calling the other?

Aucun commentaire:

Enregistrer un commentaire