对于接口,重载和泛型的问题,哪个类是最好的设计?

时间:2019-05-20 14:10:59

标签: java generics design-patterns interface

我正在尝试实现某种变压器类设计,出于这个问题的目的,我创建了另一个简单的项目,以便可以更好地进行说明。

让我们首先定义相同的类:

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

如您所见,只有3个简单的抽象类,现在我将编写一些Concretion类,以扩展这些类:

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  {
}

因此,每个抽象类都有2个具体概念。现在,我将编写一个重载相同方法的接口,该接口将接收不同的抽象类:

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

到目前为止,太好了;现在,我将添加实现该接口的相同类:

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) {
  }
}

每个实现该接口的类为每个getResponse方法提供特定的实现。

好!这是东西,我想使用该代码,我只写了一个Main方法客户端:

    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 concreteTransf1=new TransformerConcretionA();
       TransformerConcretionB concreteTransf2=new TransformerConcretionB();
       TransformerConcretionC concreteTransf3=new TransformerConcretionC();


       List<WrapperA> listWithResponses= new ArrayList();
       listWithResponses.add(concreteA);
       listWithResponses.add(concreteA2);


       listWithResponses.stream().map((e)->{
         //Depending in some logic related ,use same Concrete Transformed 
       if (whatever){
         //I want to use the First Transformer 
         //HERE How can I do for just send /accept a ConcreteClassA product
         concreteTransformer1.getResponse(e);
     }else{
         //I want to use the Second Transformer 
         //HERE How can I do for just send /accept a ConcreteClassA2 product
         concreteTransformer2.getResponse(e);

     }
    return e;
}).collect(Collectors.toList());



}

我知道TransformerConcretion1的3种getResponse方法将使用3种混凝土包装:ConcreteofA,ConcreteofB,ConcreteOfC

我知道TransformerConcretion2的3种getResponse方法将使用3种混凝土包装:ConcreteofA2,ConcreteofB2,ConcreteOfC2

所以,让我们关注这个签名:

public void getResponse(WrapperA wrapper) { }

就像我告诉你的那样,例如在上面的签名中的TransformerA中,我希望只接受ConcreteOfA类,而不是其他Concretion,并且在上述方法的TransformerB中,我将仅在参数中接收ConcreteOfA2。

我想避免在每种方法上使用CAST传递给特定具体类的参数。

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

我将需要在每个实现该可转换接口的类的每个getResponse上执行上述代码,这不是很好!

我不希望使接口通用。为什么不?因为将来,我可能会有很多Wrapper抽象类,例如:getResponse(WrapperD res) getResponse (Wrapper E res)等,所以我不想在接口的声明中传递很多具体实现。

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

我在界面中尝试了泛型方法,但无法使其正常工作。

0 个答案:

没有答案