开发者

What's the right way to design my interface when I have operations that aren't supported by all implementers?

开发者 https://www.devze.com 2023-03-23 10:34 出处:网络
I have an Interface and two Classes which are implementing the Interface. public interface MyInterface {

I have an Interface and two Classes which 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 t开发者_StackOverflow社区he 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");
}


If you still need it in the interface and it never should be called (in that implementation) I would implement make it throw an UnsupportedOperationException:

public int secondMethod() {
    throw new UnsupportedOperationException("Should not be called!");
}


If there are irrelevant methods for you:

  1. maybe you shouldn't use this interface, consider changing the design.
  2. Add stub methods (as you did).


This depends on the requirement. 1. Interface pattern is useful when every other class implementing the interface has almost same behavior, that explains why all the method implementation is mandatory.

  1. If you feel some class might need some methods and others not, probably you can got for abstract Class model, where you can extend and give the implementation which your class needs.

basically you have to take the call what solves your purpose to a greater extent.

for Ex: if 9 out of 10 methods you are implementing , then probably better idea is to just implement with return 0.


To achieve this, you will have to use an abstract parent class, instead of an interface (which might not be what you want).


Usually in these cases you create a base class that provides empty implementations of all interface methods, and extend from it. For example take a look at MouseAdapter


no there is no way, because you implement the Interface(with all methods inside). It wont be valid to implement just one method. This will break polimorphism


You are making a class that promises to any caller that it will do all the things that MyInterface offers.

Something is fundamentally wrong with the design if you cannot honour this commitment.

Put yourself in the position of the caller: what are they going to do with answer from secondMethod() ? Most likely it there's a sensible value such as 0 or 1 that you can return.

The important thing is that you do not surprise your caller, so I'm opposed to throwing a NotImplementedException.

0

精彩评论

暂无评论...
验证码 换一张
取 消