当我的操作不被所有实现者支持时,设计我的界面的正确方法是什么? [英] What's the right way to design my interface when I have operations that aren't supported by all implementers?

查看:212
本文介绍了当我的操作不被所有实现者支持时,设计我的界面的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口和两个类是实现接口。

  public interface MyInterface {
public void firstMethod();
public int secondMethod();
}

public class MyClass1 implements MyInterface {
public void firstMethod(){}
}

public class MyClass2 implements MyInterface {
public void firstMethod(){}
public int secondMethod(){}
}

MyClass1 类告诉我添加未实现的方法,因为 secondMethod 没有实现,我会这样做。但问题是,我不需要这个方法在 MyClass1



您认为最好的做法是什么?


  1. 未实现的方法,如 return 0

  2. 如果我不想实现它,还有另一种方法来解决这个问题。 li>


解决方案

您应该执行下列操作之一:


  1. 将界面分成更小的部分,并根据需要编写。这是首选方法,特别是如果您控制 MyInterface


  2. 返回最合理的默认值。 >

  3. 引发 UnsupportedOperationException







这里有一个更具说明性的例子。让我们假设你的界面如下:

  public interface Driveable {
public Position acceleration(Vector v,Time t) ;
public String getVehicleIdentificationNumber();
}



如果您的 MyClass1 实际上是 Boat ,并且没有车辆识别号码,那么您实现这一点没有意义。事实上,它的确是错的。其他客户希望你有这个值,但你不能给他们。



最好块和组成接口分成更小的片段,根据需要使用正确的切片。例如,您可以改写为:

  public interface Driveable {
public Position acceleration(Vector v,Time t );
}

public interface Vehicle extends Driveable {
public String getVehicleIdentificationNumber();
}

public class Boat implements Driveable {...}
public class Car implements Vehicle {...}

这是最好的方法,因为它可以根据需要在整个接口上分配责任。



真的在您的域中对于所有Driveables都具有车辆识别号是很重要的,这只是车辆识别号未知或不可用的特殊情况,那么您可以提供默认实现: / p>

  public String getVehicleIdentificationNumber(){
return
}

如果您的域中错误地返回了车辆识别号,那么你应该抛出一个异常:

  public String getVehicleIdentificationNumber(){
throw new UnsupportedOperationException b $ b船只不支持号码);
}


I have an Interface and two Classes wich are implementing the Interface.

public interface MyInterface {
    public void firstMethod();  
    public int secondMethod();
}

public class MyClass1 implements MyInterface  {
    public void firstMethod() {}
}

public class MyClass2 implements MyInterface  {
    public void firstMethod() {}
    public int secondMethod() {}
}

The class MyClass1 is telling me to Add unimplemented methods, because the secondMethod is not implemented, OK I will do that. But the problem is that I don't need this method in MyClass1.

In your opinion what is the best thing to do?

  1. Add the unimplemented method with something like return 0
  2. There is another way to fix this if I don't want to implement it.

解决方案

You should do one of the following:

  1. Break up the interface into smaller pieces and compose as needed. This is the preferred approach, especially if you control MyInterface.

  2. Return the most sensible default value that you can.

  3. Throw an UnsupportedOperationException.


Here's a more illustrative example. Let's say your interface looks like this:

public interface Driveable {
  public Position accelerate(Vector v, Time t);
  public String getVehicleIdentificationNumber();
}

If your MyClass1 is actually a Boat and doesn't have a vehicle identification number, then it doesn't make sense for you to implement this. In fact, it's actually wrong. Other clients expect you to have this value, but you can't give it to them.

It would be better to chunk and compose the interfaces into smaller pieces, using the right slice as necessary. For example, you might instead write this:

public interface Driveable {
  public Position accelerate(Vector v, Time t);
}

public interface Vehicle extends Driveable {
  public String getVehicleIdentificationNumber();
}

public class Boat implements Driveable { ... }
public class Car implements Vehicle { ... }

This is the best approach since it segments the responsibilities exactly as needed across the interfaces.

If it really was important in your domain for all Driveables to have a vehicle identification number, and this is just a special case where the vehicle identification number is unknown or not available, then you can provide a default implementation:

public String getVehicleIdentificationNumber() {
  return "";
}

If it would be wrong in your domain to return a vehicle identification number at all, then you should throw an exception:

public String getVehicleIdentificationNumber() {
  throw new UnsupportedOperationException("vehicle identification
    number not supported on boats");
}

这篇关于当我的操作不被所有实现者支持时,设计我的界面的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆