简体   繁体   English

依赖注入:基于接口和类的松耦合机制之间的区别?

[英]Dependency Injection: Difference between loose coupling mechanisms based on interface and class?

Suppose, I have 2 configurations. 假设我有2种配置。

First one: 第一:

interface I {
    ...
}

class A implements I {
    ...
}

class B implements I {
    ...
}

class Component {
    I i;    
    Component (I i) {
        this.i = i;
    }
}

Second one: 第二个:

class C {
    ...
}

class A extends C {
    ...
}

class B extends C {
    ...
}

class Component {
    C c;    
    Component (C c) {
        this.c = c;
    }
}

What in the difference between those two configurations which are using two different loose coupling mechanisms (based on interface and based on class)? 使用两种不同的松耦合机制(基于接口和基于类)的这两种配置之间有什么区别?

Why should I need to use interface over class? 为什么需要在类上使用接口?

An object can implements multiple interfaces but can only extends one class, so sometimes the user of your loose coupled library may not be able to extends your class. 一个对象可以实现多个接口,但只能扩展一个类,因此有时松散耦合库的用户可能无法扩展您的类。

when you extends, you incorporate the code of the superclass into yours, sometimes there is no code to incorporate so implements is better suited. 扩展时,您会将超类的代码合并到您的代码中,有时没有要合并的代码,因此工具更适合。

Finally it is not the same paradigm, when you write "B extends A" you say "I am A, I do the same things, but I can also make other things". 最后,它不是同一范式,当您编写“ B扩展A”时,您会说“我是A,我做同样的事情,但是我也可以做其他事情”。 When you write "B implements A" you say "I am B, but you can treat me as an A". 当您编写“ B实现A”时,您会说“我是B,但您可以将我视为A”。

Long story short, the only practical reason would be the first. 长话短说,唯一的实际原因将是第一个。

Mainly, using classes is a more heavy approach. 主要是,使用类是一种更繁重的方法。 Base classes usually contain concrete (non-abstract) methods. 基类通常包含具体(非抽象)方法。 And if you derive from them you will need also to inherit such concrete methods, which you may not want. 而且,如果从它们派生,则还需要继承您可能不想要的此类具体方法。

For example, imagine that you want to benefit from composition design patterns like the decorator pattern . 例如,假设您想从诸如decorator pattern之类的合成设计模式中受益。 Here, your decorator needs also to derive from the base class and will inherit the concrete methods. 在这里,您的装饰器还需要从基类派生,并将继承具体方法。 However, your decorator will not need the concrete method to function. 但是,您的装饰器将不需要具体的方法来起作用。

In summary, it is more hard/unclean to do object composition if your abstractions are class-based. 总而言之,如果您的抽象是基于类的,则很难进行对象组合。

Abstraction through interfaces on the other hand, does not force implementing classes to inherit any concrete method implementation. 另一方面,通过接口进行抽象不会强制实现类继承任何具体的方法实现。 Therefore, it is more clean. 因此,它更干净。

A hierarchy based upon interfaces is a good desing approach, because it eases implementation: You can always implement an interface, but not always you can extend a class (for example, a final class). 基于接口的层次结构是一种很好的设计方法,因为它简化了实现:您可以始终实现接口,但并不总是可以扩展类(例如,最终类)。 Or, in the other hand, not always a class' author want it to be extended. 或者,另一方面,类的作者并不总是希望对其进行扩展。

So, you are highly interested on using interfaces over classes, because in this way you may accept a wider set of implementations. 所以, 在使用接口过班,因为这样你可以接受更广泛的实现非常感兴趣。 For example: 例如:

Using classes: 使用类:

class Component
{
    AbstractList c;
    Component (AbstractList c){...}
}

... Component would accept ArrayList or Vector. ... Component将接受ArrayList或Vector。

Instead, using interfaces: 相反,使用接口:

class Component
{
    List c;
    Component (List c){...}
}

... Component would be able to accept ArrayList, Vector, CopyOnWriteArrayList... ... Component将能够接受ArrayList,Vector,CopyOnWriteArrayList ...

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

相关问题 松散耦合之间的区别-封装 - Difference between loose Coupling - Encapsulation 如何解决松散耦合/依赖注入与富域模型之间的冲突? - How can I resolve the conflict between loose coupling/dependency injection and a rich domain model? 与Class.forName()的松耦合 - Loose coupling with Class.forName() Java接口松耦合在现实世界中的优势? - Java interface loose coupling advantages in realworld? 依赖注入和@DependsOn 的区别 - Difference between Dependency Injection and @DependsOn 当一个实现类是强制性的并绑定到接口契约时,如何使用Java中的接口实现松散耦合? - How is loose coupling achieved using interfaces in Java when an implementation class is mandatory and bound to interface contract? 每个类引用相同数据时如何保持类之间的松散耦合 - How to maintain loose coupling between classes when each class is referencing the same data CDI(上下文和依赖注入)和DI(依赖注入)之间有什么区别 - What is the difference between CDI (Contexts and Dependency Injection) and DI (Dependency Injection) 从接口扩展的单例类的依赖注入 - Dependency Injection of Singleton Class that extends from Interface 创建新对象和依赖注入的区别 - Difference between creating new object and dependency injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM