简体   繁体   English

抽象类和接口在一起?

[英]Abstract class and interface together?

I have a section of my code where some classes are implementing an interface. 我有一段代码,其中一些类正在实现一个接口。

It feels correct, but there is a little duplication among the child classes - namely 3 methods. 这感觉是正确的,但是在儿童班中有一点重复 - 即3种方法。

So this is screaming out to use an abstract class. 所以这是尖叫着使用抽象类。

My question is, will there be any cons in using both abstract class and interface in the following situations: 我的问题是,在以下情况下使用抽象类和接口是否有任何缺点:

  1. Abstract class to implement the interface and child classes to extend the abstract class 抽象类实现接口和子类来扩展抽象类
  2. Child classes to extend the abstract class and implement the interface 子类扩展抽象类并实现接口

Or 要么

Should abstract classes and interfaces not be used together at all like this? 抽象类和接口是否应该像这样一起使用?

It's perfectly normal to use these two together. 将这两者结合使用是完全正常的。 Consider for instance AbstractList (implementing List ) and AbstractMap (implementing Map ) in the JDK. 考虑例如JDK中的AbstractList (实现List )和AbstractMap (实现Map )。

My knee-jerk reaction would have been to have the abstract class implement the interface and then have the concrete classes derive from it: 我的下意识反应是让抽象类实现接口,然后从中派生具体类:

abstract class Base implements TheInterface {
    /* ...shared methods... */
}

class Concrete1 extends Base { }

class Concrete1 extends Base { }

But your question raising the other possibility made me think, and I can't see much of an argument against doing it that way: 但是你提出另一种可能性的问题让我思考,而且我不能看到很多反对这样做的论点:

abstract class Base {
    /* ...shared methods... */
}

class Concrete1 extends Base implements TheInterface { }

class Concrete1 extends Base implements TheInterface { }

Further, I can see an argument for doing it that way, specifically that it removes the coupling between the abstract class and the interface. 此外,我可以看到一个参数这种方式,具体地,它消除了抽象类和接口之间的耦合。 If you have another class that needs the functionality Base provides but doesn't need to implement the interface, you have the flexibility to do that. 如果您有其他类的功能性需求Base规定,但并不需要实现该接口,您可以灵活地做到这一点。

There's also a third option: Composition. 还有第三种选择:组合。 You could not have an abstract class at all, but rather have the multiple concrete classes that implement the interface use a common helper class in their implementation: 根本没有抽象类,而是实现接口的多个具体类在其实现中使用公共帮助器类:

class Helper {
    /* ...shared methods... */
}

class Concrete1 implements TheInterface {
    /* ...uses instance of Helper */
}

class Concrete1 implements TheInterface {
    /* ...uses instance of Helper */
}

This has that same flexibility, in another form. 这具有相同的灵活性,另一种形式。

I do not think there is a rule of thumb as such. 我不认为有这样的经验法则。 When designing try and follow the SOLID principles to figure out if what you are doing is good or bad. 在设计时尝试并遵循SOLID原则来确定您所做的事情是好还是坏。 You can find these principles over here . 你可以在这里找到这些原则。 In this particular case, I would think you should ensure you are abiding by the "Open-Close Principle". 在这种特殊情况下,我认为你应该确保你遵守“开放 - 关闭原则”。

Personally I prefer the statement that an abstract class is a background for other classes, so if three other classes have something common, their common ancestor, the abstract class created only for those three other classes, should provide also code from the interface. 我个人更喜欢抽象类是其他类的背景的声明,所以如果其他三个类有共同点,那么它们的共同祖先,即仅为这三个其他类创建的抽象类,也应该提供来自接口的代码。 This would make the abstract class "complete" (in its way) providing all these properties and methods that the three classes share. 这将使抽象类“完整”(以其方式)提供三个类共享的所有这些属性和方法。

However, it finally makes no difference if all of them would implement the same interface. 但是,如果所有这些都实现相同的接口,它最终没有区别。 Making abstract class giving everything that is common is in my opinion a more clear way. 在我看来,使抽象类给出一切常见的东西是一种更清晰的方式。 It's then easier to compare classes by looking only at this what differs. 然后通过仅查看不同的类来比较类更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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