Java: Implementation of Interface with Methods passing Interface parameter

社会主义新天地 提交于 2019-12-14 02:38:59

问题


I have a given interface called IAbc, which consists of a method with a parameter of interface IXyz. I am not sure whether I should create an attribute IXyz or an attribute XyzImpl in my implementation of IAbc.

public interface IXyz { }
public class XyzImpl implements IXyz {
   public void doSomething() { ... }
}

public interface IAbc {
   public void setXyz(IXyz val);
   public IXyz getXyz();
}

Implementation 1:

public class AbcImpl1 implements IAbc {
   private XyzImpl attr;
   public void setXyz(IXyz val) {
      attr = (XyzImpl) val;
   }
   public IXyz getXyz() {
      return (IXyz) attr;
   }
   public void abc() {
      attr.doSomething();
   }
}

Implementation 2:

public class AbcImpl2 implements IAbc {
   private IXyz attr;
   public void setXyz(IXyz val) {
      attr = val;
   }       
   public IXyz getXyz() {
      return attr;
   }
   public void abc() {
      ((XyzImpl)attr).doSomething();
   }
}

Basically I only want to use my implementation XyzImpl within my implementation AbcImpl.

In any case, if I access the method abc() of one of my implementations, the accessing method needs to cast as well.

public void method() {
   AbcImpl1 abc;
   XyzImpl xyz = (XyzImpl) var.getXyz();
   xyz.abc();
}

Which way of implementation makes more sense? Casting in all classes where my implementation will be used is also not very handy. Is there any way to use only my implementation classes and not the interfaces?

Thank you in advance for your feedback!

BR, bobbel


回答1:


Just stick to interface IXyz so when you need to switch to other IXyz implementation for example from XyzImpl to XyzImpl1, you can be sure that AbcImpl will work as intended.

public class AbcImpl implements IAbc {
   private IXyz attr;
   public void setXyz(IXyz val) {
      attr = val;
   }
   public IXyz getXyz() {
      return attr;
   }
   public void abc() {
      attr.doSomething();
   }
}

If you always need access abc() method from IAbc implementation then you need to include abc() method in IAbc interface as well

public interface IAbc {
   public void setXyz(IXyz val);
   public IXyz getXyz();
   public void abc();
}

then you just need to use interface not implementation in any method calls.

public void method() {
   IAbc abc1 = new AbcImpl();
   abc1.setXyz(XyzImpl);
   IXyz xyz = abc1.getXyz();

   //same as calling abc1.getXyz().doSomething();
   abc1.abc();
}



回答2:


Both ways are bad: if your class requires the parameter to have .abc method, and XYZ interfaces does not have it, you should not advertise, that you accept parameters of type XYZ. That is the whole purpose of having the type system in the first place. If you don't subscribe to that notion, you are better off writing code in ruby or perl.

Bottom line: either add abc to XYZ, or use XYZImpl in your ABCImpl class.




回答3:


Your interface IXyz has no methods in it, so you need to think about why you need this interface.Also if you need only an XYImpl why does IAbc get and set IXyz? Usually "programming to an interface over implementation" is recommended but with no methods in IXyz, you are not achieving that. See this question as to why it is recommended to use inteface over implementation : What does it mean to "program to an interface"? and http://www.artima.com/lejava/articles/designprinciples.html



来源:https://stackoverflow.com/questions/36074143/java-implementation-of-interface-with-methods-passing-interface-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!