lundi 20 mai 2019

Whick is the best Class design for a problem about Interfaces,Overloading and Generics?

I am triyng to implement some kind of transformer class design,for the purpose of this question,i created another simple project ,so i can illustrate it better.

Lets define same classes first :

public abstract class WrapperA {

   private String abstractField="Wrapper A";

   public String getAbstractField() {
      return abstractField;
   }

   public void setAbstractField(String abstractField) {
      this.abstractField = abstractField;
   }
}
public abstract class WrapperB {

   private String abstractField="Wrapper B";

   public String getAbstractField() {
      return abstractField;
   }

   public void setAbstractField(String abstractField) {
      this.abstractField = abstractField;
   }
}
public abstract class WrapperC {

   private String abstractField="Wrapper C";

   public String getAbstractField() {
      return abstractField;
   }

   public void setAbstractField(String abstractField) {
      this.abstractField = abstractField;
   }
}

As you can see ,just 3 simply Abtract Classes , now i will write some Concretion Classes ,extending those classes:

public class ConcreteOfA extends WrapperA  {
}
public class ConcreteOfA2 extends WrapperA  {
}
public class ConcreteOfB extends WrapperB  {
}
public class ConcreteOfB2 extends WrapperB {
}
public class ConcreteOfC extends WrapperC  {
}
public class ConcreteOfC2 extends WrapperC  {
}

So ,i have 2 concretions for each Abstract Class. Now i will write an interface overloading the same method,where it receives a different Abtract Class :

public interface ITransformable {
   void getResponse(WrapperA wrapper);
   void getResponse(WrapperB wrapper);
   void getResponse(WrapperC wrapper);

}

So far ,so god,now i will add sames classes implementing that interface :

public class TransformerConcretionA implements ITransformable  {

   @Override
   public void getResponse(WrapperA wrapper) {
   }

   public void getResponse(WrapperB wrapper) {
   }

  public void getResponse(WrapperC wrapper) {
  }
}

public class TransformerConcretionB implements ITransformable {

   @Override
   public void getResponse(WrapperA wrapper) {
   }

   public void getResponse(WrapperB wrapper) {
   }

  public void getResponse(WrapperC wrapper) {
  }
}
public class TransformerConcretionC implements ITransformable {

   @Override
   public void getResponse(WrapperA wrapper) {
   }

   public void getResponse(WrapperB wrapper) {
   }

  public void getResponse(WrapperC wrapper) {
  }
}

Ok!Here is the thing,i i want to use that code,i just write a Main method client:

    public static void main(String[] args) {
      //Create Some Concretion Classes  
       ConcreteOfA concreteA=new ConcreteOfA();
       ConcreteOfB concreteB=new ConcreteOfB();
       ConcreteOfC concreteC=new ConcreteOfC();

       ConcreteOfA2 concreteA2=new ConcreteOfA2();
       ConcreteOfB2 concreteB2=new ConcreteOfB2();
       ConcreteOfC2 concreteC2=new ConcreteOfC2();

       //Create Concrete Transformers;
       TransformerConcretionA concreteTransfA=new TransformerConcretionA();
       TransformerConcretionB concreteTransfB=new TransformerConcretionB();
       TransformerConcretionC concreteTransfC=new TransformerConcretionC();


       //So  if i do this,it works
      concreteTransformerA.getResponse(concreteA); 
      concreteTransformerB.getResponse(concreteA2);

}

I know that TransformerConcretionA ,for their 3 getResponse Methods will use 3 concrete Wrappers:ConcreteofA ,ConcreteofB ,ConcreteOfC

I know that TransformerConcretionB ,for their 3 getResponse Methods will use 3 concrete Wrappers:ConcreteofA2 ,ConcreteofB2 ,ConcreteOfC2

SO! Lets focus on this signature:

public void getResponse(WrapperA wrapper) { }

Like i told you,in TransformerA for example in the above signature i will only work with ConcreteOfAClass,not other Concretion,and in TransformerB in the above method i will only receive ConcreteOfA2 in the parameter.

What i want??

I want to aovid on each method to CAST the argument to the specifyc concrete class.

 public void getResponse(WrapperA wrapper) { 
     ((ConcreteOfA) wrapper)
  }
 public void getResponse(WrapperB wrapper) { 
     ((ConcreteOfB) wrapper)
  }

I will need to do the above code on each getResponse ,on each class that implements this transformable interface.

What i dont want it s to make the Interface generic. Why not?

Because in the future,i could have a LOT of Wrapper Abtract Classes,for example: getResponse(WrapperD res) getResponse (Wrapper E res) ,

and so on ,so i dont want to pass a lot of Concrete implementation in the declaration of the Interface

    public interface ITransformermable<V,T,D,E,F,etc,etc,etc> (I dont 
     want that)

I tried Generic method in the interface ,but i cant make it work like i expect.

I really need help so i can have the best design to solve this problem. Please,let me know if something it s not clear. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire