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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19483626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:37:58  来源:igfitidea点击:

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

javaspringoopdesign-patterns

提问by Horse Voice

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.

当我们无论如何都要创建一个实现类时,松散耦合如何与接口相关联?实现类被迫实现接口中定义的所有方法。我不明白这如何允许失去耦合?我是面向对象编程和软件设计的新手,所以如果您能对这个主题有所了解,那将非常有帮助。一个例子完全是锦上添花。

采纳答案by Philipp

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.

请注意,接口并不是实现组件松散耦合的唯一方法。松散耦合只是意味着组件能够一起工作,而无需对彼此的内部工作做任何假设。您这样做是因为您的组件越多将彼此视为黑匣子,在不影响任何其他组件的情况下对一个组件进行更改就越容易。接口可以是实现这一目标的一种工具,但它们既不是必需的,也不是唯一值得一提的工具。

回答by Sam Turtel Barker

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. The object which needs the person does not care how the person is found or loaded just that it is.

可以通过读取 CSV 文件或查询数据库来实现。需要这个人的对象并不关心这个人是如何被发现或加载的。

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 文件。

回答by Ray Tayek

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_principleand http://en.wikipedia.org/wiki/Open/closed_principle

参见http://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://en.wikipedia.org/wiki/Open/closed_principle

回答by Neeraj

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. ex: one can add more methods into B or anything. 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. Ex : class A { public void add( B b){ // Implementation } } class B{

进入A类我们需要有B类的Object。如果B直接暴露到A中,就意味着存在紧耦合。例如:可以在 B 或任何东西中添加更多方法。这意味着可以根据 B 的更多曝光来更改 A 的行为。但是如果 B 类正在实现任何接口,并且我们将接口的 ref 传递给 A。意味着 B 类中的任何更改进一步用户 A 类都不会打扰,因为我们使用用于访问 B 的接口的 ref 并且仅获得必需的访问权限。例如:class A { public void add( B b){ // 实现 } } class 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.