简体   繁体   English

当一个实现类是强制性的并绑定到接口契约时,如何使用Java中的接口实现松散耦合?

[英]How is loose coupling achieved using interfaces in Java when an implementation class is mandatory and bound to interface contract?

How is loose coupling associated with interfaces when we are bound to create an implementation class regardless? 无论如何,当我们必须创建一个实现类时,松散耦合如何与接口相关联? The implementation class is forced to implement all those methods defined in the interface. 实现类被强制实现接口中定义的所有方法。 I don't understand how this allows for lose coupling? 我不明白这是如何允许失去耦合的? I'm new to object oriented programming and software design so if you could shed some light on this topic it would super helpful. 我是面向对象编程和软件设计的新手,所以如果你能对这个主题有所了解,那将会非常有用。 An example would totally be icing on the cake. 一个例子就是锦上添花。

The key point is that an interface doesn't just allow you to write one class which implements it, it allows you to write several. 关键是接口不仅允许你编写一个实现它的类,它允许你写几个。

When you have code which interacts with a class by using an interface, that code is able to work together with any class which implements said interface, regardless of how it implements it. 当您拥有通过使用接口与类交互的代码时,该代码能够与实现所述接口的任何类一起工作,而不管它如何实现它。 That allows you to feed different classes to the same code without having to modify it. 这允许您将不同的类提供给相同的代码而无需修改它。

Please note that interfaces are not the only way to reach a loose coupling of components. 请注意,接口不是达到松散耦合组件的唯一方法。 Loose coupling just means that components are able to work together without assuming anything about the internal workings of each other. 松散耦合仅意味着组件能够一起工作而不会假设彼此的内部工作。 You do that because the more your components treat each other as black boxes, the easier it becomes to do changes at one component without affecting any others. 这样做是因为您的组件越多地将对方视为黑盒子,就越容易在一个组件上进行更改而不影响任何其他组件。 Interfaces can be one tool to work towards this goal, but neither are they required, nor are they the only tool which is worth mentioning in this regard. 接口可以是实现这一目标的一种工具,但它们都不是必需的,它们也不是唯一值得一提的工具。

The implementing class is able to choose HOW to implement the functionality. 实现类能够选择如何实现该功能。

public interface PersonRepository {
    Person getPerson(String name);
}

Could be implemented by reading through a CSV file or by querying a database. 可以通过读取CSV文件或查询数据库来实现。 The object which needs the person does not care how the person is found or loaded just that it is. 需要这个人的对象并不关心这个人是如何被发现或装载的。

Hence it is deemed to be loosely coupled. 因此,它被认为是松散耦合的。

If it was tightly coupled it would need to know how to construct a SQL query or read a CSV file. 如果它是紧密耦合的,则需要知道如何构造SQL查询或读取CSV文件。

the client code is coupled to the interface. 客户端代码耦合到接口。 it is not coupled to the implementation. 它没有与实现相结合。 you can change t he implementation without compiling the client code or the interface. 您可以在不编译客户端代码或界面的情况下更改实现。

see http://en.wikipedia.org/wiki/Dependency_inversion_principle and http://en.wikipedia.org/wiki/Open/closed_principle http://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://en.wikipedia.org/wiki/Open/closed_principle

Might below explanation can answer this : 可能在下面的解释可以回答这个:

Into class A we need to have Object of class B. If directly exposure of B into A is there means there is Tight coupling. 进入A类,我们需要B类的对象。如果将B直接暴露在A中则意味着存在紧耦合。 ex: one can add more methods into B or anything. 例如:可以在B或其他任何东西中添加更多方法。 Which means Behavior of A can be changed based on more exposure of B. But if B class is implementing any Interface and we are passing that ref of Interface into A. means whatever changes in B class further user A class is not bother because we use ref of Interface for accessing B and got only required access. 这意味着A的行为可以基于B的更多暴露而改变。但是如果B类正在实现任何接口并且我们将接口的ref传递给A.意味着B类中的任何变化进一步用户A类不会因为我们使用而烦恼用于访问B的接口的ref,仅获得所需的访问权限。 Ex : class A { public void add( B b){ // Implementation } } class B{ 例如:A类{public void add(B b){// Implementation}} B类{

    }
            this is Tight coupling. Because user can make any changes to class B which are directly exposed to class A and this defines Tight Coupling(Greater the exposure of depending Object , more Tight Coupling).  To resolve this one refer :
    Interface L{

    }
    class B implements L{}
    class A{
        public void add(L  b){
            // Implementation
        }
    }

        Since we are passing the ref of Interface L, adding changes into Implementation of B will not make any difference to class A.

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

相关问题 Java接口松耦合在现实世界中的优势? - Java interface loose coupling advantages in realworld? 松耦合是否可以通过任何其他方式而不是使用父类引用变量来实现,通常不是在我的代码中专门进行? - ls loose coupling can be achieved by any other manner rather than using parent class reference variable, in general not specifically in mine code? 使用ORM时耦合数据库交互的耦合程度如何 - How loose coupling DB interaction when using ORM 依赖注入:基于接口和类的松耦合机制之间的区别? - Dependency Injection: Difference between loose coupling mechanisms based on interface and class? 出现异常时如何使用接口将接口与技术实现分开 - How to separate interface from technical implementation using Interfaces when there are Exceptions 每个类引用相同数据时如何保持类之间的松散耦合 - How to maintain loose coupling between classes when each class is referencing the same data 难以理解通过接口的松耦合 - Difficulty in understanding Loose coupling via Interfaces 什么是oop中的松耦合和紧耦合(java) - what is loose coupling and tight coupling in oop ( java ) Java 代码的紧耦合和松耦合示例 - tight coupling and loose coupling examples with Java code 与Class.forName()的松耦合 - Loose coupling with Class.forName()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM