简体   繁体   中英

Optimization of interface extending interface in java

I have an interface A with two methods:

public interface A{
    method One();
    method Two();
}

And I have two options for interface B :

Case 1:

public interface B{
    method One();
    method Two();
    method Three();
}

Case 2:

public interface B extends A{
    method Three();
}

Can anyone tell me which of the above two cases is better to implement and why? Which are the downsides of Case 2?

Edit: In interface B, I need both the methods of interface A. So, I thought extending would be much better. There are some classes where I need method One and Two only, there I implement interface A. And where I need method One, Two and Three, I implement interface B.

It depends on what you need to do. If method Three() introduces functionality that from the design point of view should be encapsulated into another unit (such as interface or class) then use 2 if it's similar functionality use 1.

EDIT: There is also one more option: interfaces don't have extend each other and concrete class may implement them both, but again - depends on what you need

EDIT2: After your edit it still doesn't make much of a difference. Still class can implement two interfaces or one and still you can use polymorphic assignment on both classes. For example if class concreteA implements interfaceA and clas concreteB implements interfaceB or class concreteB implements interfaceA, interfaceB you can still do InterfaceA classB = new concreteB();

This depends on your logic. Consider these two pairs of interfaces:

interface House {
    int doorCount();
    int windowCount();
}

interface Automobile {
    int doorCount();
    int windowCount();
    int wheelCount();
}

Nobody in his right mind would derive Automobile by extending a house, because an automobile is not a house with wheels.

On the other hand, in situation like this

interface House {
    int doorCount();
    int windowCount();
}

int Mansion extends House {
    int poolCount();
}

deriving from House makes sense, because Mansion is a kind of House with "additional features".

If you go with Case 1, and you already have client code written against the type A , it will fail to work with instances of classes which implement B , and for a purely red-tape reason: B is conceptually an extension of A , but modeled in the Java type system as a disparate type.

Example:

static void existingMethod(A a) {
  ...work with a ...
}

class ImplB implements B { ... }

public static void main(String... args) {
   B b = new B();
   existingMethod(b); // compile error for Case 1; Case 2 compiles fine.
}

Therefore it would make much more sense to have your B type extend A .

One way is to look at the type of relationship between A and B .

If B "is a" A , then it would make more sense to go with case 2.

It depends on what you use the interfaces for. If all interfaces that use interface A also use interface B, then you may as well use case 1 and get rid of interface A all together. Otherwise, there is no reason to duplicate code. Duplicating code means that if you change something, you have to change it everywhere, instead of just one place.

如果你有A并想要使用它,你可以去B扩展A.如果你只是因为重用方法声明而从A扩展B,那么你的具体类必须实现所有3种方法,这就是缺点(没什么用)当你只专注于B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM