Java 松耦合和紧耦合将在哪些情况下用作真实场景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19976273/
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
Where loose and tight coupling would be used as a real scenario?
提问by Achiever
I have have been reading this article: Difference between loose and tight coupling in Java?
我一直在阅读这篇文章: Java 中松散耦合和紧耦合的区别?
After I read this I am still confused and do not understand it at all. I am a Java beginner.
读完之后,我还是一头雾水,完全不明白。我是Java初学者。
What is the importance of coupling in Java and why? Where, how and when would it be needed while designing code?
Java 中耦合的重要性是什么?为什么?在设计代码时在哪里、如何以及何时需要它?
采纳答案by benka
Tight Coupling
紧耦合
In complex cases the logic of one class will call the logic of another class just to provide the same service
If this happen there is the so called tight-coupling between the two classes.
In this case the first class that wants to call the logic from the second class will have to create an object from the second class
在复杂的情况下,一个类的逻辑会调用另一个类的逻辑来提供相同的服务
如果发生这种情况,两个类之间就会出现所谓的紧耦合。
在这种情况下,想要从第二个类调用逻辑的第一个类必须从第二个类创建一个对象
Example:we have two classes first is traveller
and the second is a car
. Traveller
class is calling logic of car
class; in this case traveller class creates an object of car class.
示例:我们有两个类,第一个是 is traveller
,第二个是 a car
。Traveller
class 调用类的逻辑car
;在这种情况下,traveler 类创建了一个汽车类对象。
This will mean the car
class will depend on the traveller
object.
这意味着car
类将依赖于traveller
对象。
Public class Traveller {
Car c = new Car();
Public void startJourney() {
c.move();
}
}
Public class Car {
Public void move(){
...
}
}
Here traveller
object is tightly coupledwith car
object.
If traveller
wants to change from car
to plane
then the whole traveller
class has to be changed like the following:
这里traveller
对象与对象紧密耦合car
。
如果traveller
要从 更改为car
,plane
则整个traveller
班级都必须更改,如下所示:
Public class Traveller {
Plane p = new Plane();
Public void startJourney() {
p.move();
}
}
Public class Plane {
Public void move(){
...
}
}
Loose Coupling
松耦合
Our main object,
traveller
in this case will allow an external entity, a so calledcontainer
to provide theobject
. Sotraveller
doesn't have to create an own class from thecar
orplane
object it will get it from thecontainer
When a object allow dependency injection mechanism.
The external entity, the
container
can inject thecar
orplane
object based on a certain logic, a requirement of thetraveller
.
traveller
在这种情况下,我们的主要对象将允许外部实体,即所谓container
的提供object
. 所以traveller
不必从car
orplane
对象创建自己的类,它会从container
当一个对象允许依赖注入机制。
外部实体,
container
可以根据一定的逻辑注入car
或plane
对象,traveller
.
Example:
例子:
Public class Traveller {
Vehicle v;
Public void setV(Vehicle v) {
this.V = V;
}
Public void startJourney() {
V.move();
}
}
Here traveller
class injects either a car
or a plane
object. No changes required in traveller
class if we would like to change the dependency from car to a plane.
这里的traveller
类注入一个car
或一个plane
对象。traveller
如果我们想将依赖从汽车更改为飞机,则不需要在课堂上进行更改。
Traveller
class took a vehicle reference, so an external object (Container) can inject either car
object or plane
object, depends on requirement of traveller
.
Traveller
类接受了车辆引用,因此外部对象(容器)可以注入car
对象或plane
对象,具体取决于traveller
.
回答by Yubaraj
Tight-Coupling:-
紧耦合:-
While creating complex application in java, the logic of one class will call the logic of another class to provide same service to the clients.
If one class calling another class logic then it is called collaboration.
When one class is collaborating with another class then there exists tight-coupling between the two classes.
If one class wants to call the logic of a second class then they first class need an object of second class it means the first class create an object of second class.
For example, if we have two classes called traveller and car, traveller class is calling logic of car class; in this case traveller class creates an object of car class.
In the above traveller class and car classes, car class object of dependency for traveller object.
在java中创建复杂的应用程序时,一个类的逻辑会调用另一个类的逻辑来为客户端提供相同的服务。
如果一个类调用另一个类逻辑,则称为协作。
当一个班级与另一个班级合作时,两个班级之间存在紧耦合。
如果一个类想要调用第二个类的逻辑,那么他们的第一个类需要第二个类的对象,这意味着第一个类创建第二个类的对象。
比如我们有两个类,traveler和car,traveler类就是调用car类的逻辑;在这种情况下,traveler 类创建了一个汽车类对象。
在上面的traveler类和car类中,car类对象为traveler对象的依赖。
Example:-
例子:-
- In the above example traveller object is tightly coupled with car object because in place car object if you want to use bike object then, we need to make changes in Traveller class
- 在上面的例子中,traveler 对象与 car 对象紧密耦合,因为如果你想使用自行车对象,那么我们需要在 Traveler 类中进行更改
Example :-
例子 :-
Loose-Coupling:-
松耦合:-
In Loose-Coupling, when one object is depending on another class object, some external entity will provide that dependency object to the main object that external object we call as a Container.
In order to get loose-coupling between objects the following two rules are required
The classes should follow POJI/POJO model.
Apply dependency injection mechanism.
在松耦合中,当一个对象依赖于另一个类对象时,一些外部实体会将该依赖对象提供给我们称之为容器的外部对象的主对象。
为了在对象之间实现松耦合,需要以下两条规则
这些类应该遵循 POJI/POJO 模型。
应用依赖注入机制。
For example:-
例如:-
In the above traveler class, an external entity injects either car (or) Bike object.
In traveler, these are no changes required we are shifting the dependency from car to a Bike.
In the above traveler class, we are token vehicle reference, so that an external object (Container) can injects either car object (or) Bike object, depends on requirement if a traveler.
In spring frame work, spring container follows dependency injection mechanism and injects the dependency objects required for a main object.
Spring frame work is much success because of one of the main reason is it promotes Loose-Coupling between the objects.
在上面的 Traveler 类中,外部实体注入了汽车(或)自行车对象。
在旅行者中,这些不需要改变,我们正在将依赖从汽车转移到自行车。
在上面的 traveler 类中,我们标记了车辆引用,因此外部对象(Container)可以注入 car 对象(或)Bike 对象,这取决于旅行者的需求。
在 spring 框架中,spring 容器遵循依赖注入机制,注入一个主对象所需的依赖对象。
Spring 框架工作非常成功,主要原因之一是它促进了对象之间的松耦合。
Source:-Tight-coupling and Loose-coupling between objects
来源:-对象之间的紧耦合和松耦合