什么是oop中的松耦合和紧耦合(java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20445939/
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
what is loose coupling and tight coupling in oop ( java )
提问by vikas singh
i have some confusion regarding loose coupling and tight coupling in java.as i know loose coupled means least information about each other and tight coupled means dependency.as we know loose coupling can achieve through interface implementation and inheritance make tight couple.
我对java中的松耦合和紧耦合有些困惑。因为我知道松耦合意味着彼此之间的信息最少,而紧耦合意味着依赖。因为我们知道松耦合可以通过接口实现和继承来实现紧密耦合。
for example:
例如:
1) A (interface)<--------------------- B (class)
1)A(接口)<--------------------- B(类)
2) C ( class ) <--------------------- D (class)
2)C(班级)<--------------------- D(班级)
suppose these four classes is part of my entire application, making change in B or D do not make any impact on application(for running point of view). removing any method or variables from A or C required so many change in application. all right score is 2-2, but adding new method in C or A is different. if i added new method in C do not impact on application but in adding in A , at least i have to override this method in B and all the classes that implement interface A. so how it's loose coupling at least in this scenario. my doubt is, is inheritance give always tight coupling.i learned Inheritance is one of powerful tool of OOP.for designing if a class follow the "is a relationship" then use inheritance. loose coupling means less information about each other. A and C both don't know in future which class is going to implements or extends,but after adding B and D now B is not dependent on A because it's all method's are abstract, but D can also override the inheriting feature.
假设这四个类是我整个应用程序的一部分,在 B 或 D 中进行更改不会对应用程序产生任何影响(从运行的角度来看)。从 A 或 C 中删除任何方法或变量需要在应用程序中进行如此多的更改。好吧,分数是 2-2,但在 C 或 A 中添加新方法是不同的。如果我在 C 中添加新方法不会影响应用程序但在 A 中添加,至少我必须在 B 和所有实现接口 A 的类中覆盖此方法。那么它至少在这种情况下是松耦合的。我的疑问是,继承总是紧密耦合。我了解到继承是 OOP 的强大工具之一,用于设计类是否遵循“是关系”然后使用继承。松散耦合意味着彼此之间的信息较少。A 和 C 都没有
采纳答案by Tim B
Inheritance does not always give tight coupling - since the class you are inheriting provides a defined way to do so through which methods it declares as being private, protected and public.
继承并不总是提供紧密耦合 - 因为您继承的类提供了一种定义的方式来这样做,通过它声明为私有、受保护和公共的方法。
A good example of this is many of the abstract classes provided by various APIs which implement some of the boiler plate functionality of an interface for you and allow you to focus on your own requirements.
一个很好的例子是各种 API 提供的许多抽象类,它们为您实现了接口的一些样板功能,并允许您专注于自己的需求。
Basic examples include the "adaptor" classes in swing which provide "no-op" implementations of all of the methods in the interface. More advanced examples actually provide standard implementations of some of the requirement of the interface.
基本示例包括 Swing 中的“适配器”类,它提供接口中所有方法的“无操作”实现。更高级的示例实际上提供了接口某些要求的标准实现。
Exactly what is tight coupling really is very much a judgement call with many things obviously being tightly coupled, others being obviously loosely coupled - and then a large grey area in between.
究竟什么是紧耦合实际上是一种判断,许多事物显然是紧耦合的,而其他事物显然是松耦合的——然后是中间的大灰色区域。
回答by zapl
as we know loose coupling can achieve through interface implementation and inheritance make tight couple.
我们知道松耦合可以通过接口实现和继承来实现紧密耦合。
I think you got that wrong. "coupling" is usually about 2 different classes that know each other either by their concrete class or just by some interface.
我想你弄错了。“耦合”通常是大约 2 个不同的类,它们通过它们的具体类或仅通过某个接口相互了解。
Let's say 2 classes A and B need to comunicate with each other.
假设 2 个类 A 和 B 需要相互通信。
A <--knows--> B
Methods in A would have some parameter B and methods in B have a parameter of type A. E.g. like
A 中的方法有一些参数 B,B 中的方法有一个 AEg 类型的参数,如
class A {
public void talkTo(B b) {}
}
Now that's a tight coupling between A and B because every change you do to these classes can make changes in the other class necessary.
现在这是 A 和 B 之间的紧密耦合,因为您对这些类所做的每一次更改都可能对其他类进行必要的更改。
If you do it loosely coupled they both expose themselves through some interface. ("interface" can mean abstract class too - that's a choice the respecive side.)
如果你做松散耦合,它们都会通过一些接口暴露自己。(“接口”也可以表示抽象类 - 这是各自的选择。)
IA <-- A
^ |
\ /
X < loose coupling between the A side and the B side
/ \
v |
IB <-- B < pretty tight coupling betwen IB and B
and communication between them goes via those interfaces
它们之间的通信通过这些接口进行
class A implements IA {
public void talkTo(IB b);
}
class B implements IB {
public void talkTo(IA a);
}
The dependency between A and IA (that's what you seem to look at) is not what tight vs loose coupling is primarily about. There is some similarity but loose coupling doesn't mean that you should implement an interface vs. extend an abstract class. It's usually better to implement just an interface though.
A 和 IA 之间的依赖关系(这就是您所看到的)不是紧耦合与松耦合的主要关系。有一些相似之处,但松散耦合并不意味着您应该实现接口而不是扩展抽象类。不过,通常只实现一个接口会更好。
If you can replace an "IS A" relation with a "HAS A" relation you do essentially the same. You decouple yourself (e.g. you are A) from a concrete implementation and only need to depend on the encapsulated other side (e.g. from the B side). Inheritance is indeed a very powerful feature but it is often misused.
如果您可以用“HAS A”关系替换“IS A”关系,那么您所做的基本相同。您将自己(例如,您是 A)与具体实现分离,只需要依赖封装的另一端(例如,从 B 端)。继承确实是一个非常强大的功能,但它经常被滥用。
回答by Prashant Kumar
there are three concerns or layers...implementation concern,object creation concern and usage concern.when we program to an interface not to an implementation we can achieve loose coupling..means any change in the implementation layer will have least effect on the object creation layer............
存在三个关注点或层...实现关注点、对象创建关注点和使用关注点。当我们针对接口而不是实现进行编程时,我们可以实现松耦合..意味着实现层的任何更改对对象的影响最小创建层………………
回答by Sayat Satybald
Short Introduction Loose and Tight Coupling
简短介绍 松紧耦合
Loose Coupling means reducing dependencies of a class that use a different class directly. In tight coupling, classes and objects are dependent on one another. In general, tight coupling is usually bad because it reduces flexibility and re-usability of code and it makes changes much more difficult and impedes testability etc.
松耦合意味着减少直接使用不同类的类的依赖关系。在紧耦合中,类和对象相互依赖。一般来说,紧耦合通常是不好的,因为它降低了代码的灵活性和可重用性,并且使更改变得更加困难并阻碍了可测试性等。
Tight Coupling
紧耦合
A Tightly Coupled Object is an object that needs to know quite a bit about other objects and are usually highly dependent on each other's interfaces. Changing one object in a tightly coupled application often requires changes to a number of other objects. In a small application we can easily identify the changes and there is less chance to miss anything. But in large applications these inter-dependencies are not always known by every programmer or there is a chance of overlooking changes. But each set of loosely coupled objects are not dependent on each other.
紧耦合对象是一个需要了解其他对象的对象,并且通常高度依赖于彼此的接口。在紧密耦合的应用程序中更改一个对象通常需要更改多个其他对象。在一个小型应用程序中,我们可以轻松识别更改,并且遗漏任何内容的机会更少。但是在大型应用程序中,每个程序员并不总是知道这些相互依赖关系,或者有可能忽略更改。但是每组松散耦合的对象并不相互依赖。
回答by KARSHIL SHETH
General Answer & Difference
一般答案和差异
Loosely Coupled Configuration
松耦合配置
- Each processor has its own memory module.
- It can't encounter memory conflict.
- It has message transfer system.
- Low Data Rate.
- Less Expensive.
- Dynamic Binding.
- Implicit Upgrade.
- Platform Independent.
- Asynchronous Communication Style.
- 每个处理器都有自己的内存模块。
- 它不会遇到内存冲突。
- 它有消息传输系统。
- 低数据速率。
- 不会那么贵。
- 动态绑定。
- 隐式升级。
- 平台独立。
- 异步通信方式。
Tightly Coupled Configuration
紧耦合配置
- Processor have shared memory.
- It experiences more memory conflict.
- It has interconnection network.
- High Data rate.
- More Expensive.
- Strong type System.
- Statically Binding.
- Explicit Upgrades.
- Platform Dependencies.
- Synchronous Communication Style.
- 处理器具有共享内存。
- 它经历了更多的内存冲突。
- 它有互连网络。
- 高数据速率。
- 更贵。
- 强类型系统。
- 静态绑定。
- 显式升级。
- 平台依赖。
- 同步沟通方式。