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

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

Where loose and tight coupling would be used as a real scenario?

java

提问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 travellerand the second is a car. Travellerclass is calling logic of carclass; in this case traveller class creates an object of car class.

示例:我们有两个类,第一个是 is traveller,第二个是 a carTravellerclass 调用类的逻辑car;在这种情况下,traveler 类创建了一个汽车类对象。

This will mean the carclass will depend on the travellerobject.

这意味着car类将依赖于traveller对象。

Public class Traveller {
    Car c = new Car();
    Public void startJourney() {
        c.move();
    }
}

Public class Car {
    Public void move(){
        ...
    }
}

Here travellerobject is tightly coupledwith carobject.
If travellerwants to change from carto planethen the whole travellerclass has to be changed like the following:

这里traveller对象与对象紧密耦合car
如果traveller要从 更改为carplane则整个traveller班级都必须更改,如下所示:

Public class Traveller {
    Plane p = new Plane();
    Public void startJourney() {
        p.move();
    }
}

Public class Plane {
    Public void move(){
        ...
    }
}

Loose Coupling

松耦合

  • Our main object, travellerin this case will allow an external entity, a so called containerto provide the object. So travellerdoesn't have to create an own class from the caror planeobject it will get it from the container

  • When a object allow dependency injection mechanism.

  • The external entity, the containercan inject the caror planeobject based on a certain logic, a requirement of the traveller.

  • traveller在这种情况下,我们的主要对象将允许外部实体,即所谓container的提供object. 所以traveller不必从carorplane对象创建自己的类,它会从container

  • 当一个对象允许依赖注入机制。

  • 外部实体,container可以根据一定的逻辑注入carplane对象,traveller.

Example:

例子:

Public class Traveller {
    Vehicle v;

    Public void setV(Vehicle v) {
        this.V = V;
    }
    Public void startJourney() {
        V.move();
    }
}

Here travellerclass injects either a caror a planeobject. No changes required in travellerclass if we would like to change the dependency from car to a plane.

这里的traveller类注入一个car或一个plane对象。traveller如果我们想将依赖从汽车更改为飞机,则不需要在课堂上进行更改。

Travellerclass took a vehicle reference, so an external object (Container) can inject either carobject or planeobject, depends on requirement of traveller.

Traveller类接受了车辆引用,因此外部对象(容器)可以注入car对象或plane对象,具体取决于traveller.

回答by Yubaraj

Tight-Coupling:-

紧耦合:-

  1. 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.

  2. If one class calling another class logic then it is called collaboration.

  3. When one class is collaborating with another class then there exists tight-coupling between the two classes.

  4. 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.

  5. 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.

  6. In the above traveller class and car classes, car class object of dependency for traveller object.

  1. 在java中创建复杂的应用程序时,一个类的逻辑会调用另一个类的逻辑来为客户端提供相同的服务。

  2. 如果一个类调用另一个类逻辑,则称为协作。

  3. 当一个班级与另一个班级合作时,两个班级之间存在紧耦合。

  4. 如果一个类想要调用第二个类的逻辑,那么他们的第一个类需要第二个类的对象,这意味着第一个类创建第二个类的对象。

  5. 比如我们有两个类,traveler和car,traveler类就是调用car类的逻辑;在这种情况下,traveler 类创建了一个汽车类对象。

  6. 在上面的traveler类和car类中,car类对象为traveler对象的依赖。

Example:-

例子:-

Travellerenter image description here

游客在此处输入图片说明

  • 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 :-

例子 :-

travellor 1car1

旅行者 1车1

Loose-Coupling:-

松耦合:-

  1. 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.

  2. In order to get loose-coupling between objects the following two rules are required

  3. The classes should follow POJI/POJO model.

  4. Apply dependency injection mechanism.

  1. 在松耦合中,当一个对象依赖于另一个类对象时,一些外部实体会将该依赖对象提供给我们称之为容器的外部对象的主对象。

  2. 为了在对象之间实现松耦合,需要以下两条规则

  3. 这些类应该遵循 POJI/POJO 模型。

  4. 应用依赖注入机制。

For example:-

例如:-

interface

界面

travellor 2

旅行者2

  • 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

来源:-对象之间的紧耦合和松耦合