简体   繁体   English

Java 代码的紧耦合和松耦合示例

[英]tight coupling and loose coupling examples with Java code

I am aware of the distinction between tight coupling and loose coupling, according to this articles: https://www.upgrad.com/blog/loose-coupling-vs-tight-coupling-in-java/根据这篇文章,我知道紧耦合和松耦合之间的区别: https://www.upgrad.com/blog/loose-coupling-vs-tight-coupling-in-java/

What I do not understand is the examples it uses.我不明白的是它使用的例子。

For loose coupling, the Java code:对于松耦合,Java 代码:

 class Volume {

   public static void main(String args[]) {

        Cylinder b = new Cylinder(25, 25, 25);

           System.out.println(b.getVolume());

   }

}

final class Cylinder {

    private int volume;

    Cylinder(int length, int width, int height) {

             this.volume = length * width * height;

    }

    public int getVolume() {

             return volume;

    }

}

For tight coupling, the Java code:对于紧密耦合,Java 代码:

class Volume {

   public static void main(String args[]) {

        Cylinder b = new Cylinder(15, 15, 15);

           System.out.println(b.volume);

   }}

 class Cylinder {

   public int volume;

   Cylinder(int length, int width, int height) {

           this.volume = length * width * height;  }}

Can anyone please explain how does the second code make the two classes (Volume and Cylinder) bound together (tightly coupled)?谁能解释第二个代码如何使两个类(体积和圆柱体)绑定在一起(紧密耦合)? Or what makes the first code loosely coupled?或者是什么让第一个代码松耦合? Thanks.谢谢。

Generally, in OOP you want to declare your class properties as private so you can encapsulate them.通常,在 OOP 中,您希望将 class 属性声明为private ,以便您可以封装它们。 Doing this, you don't direct have access to them and you cannot change them by mistake.这样做,您不能直接访问它们,也不能错误地更改它们。 This is intended for code maintenance reasons, and it is especially useful when you have multiple developers that have access to the codebase, as you create a barrier between important class properties and the next developer.这是出于代码维护的原因,当您有多个可以访问代码库的开发人员时特别有用,因为您在重要的 class 属性和下一个开发人员之间创建了障碍

This thing creates 2 problems though:这件事虽然产生了两个问题:

  • You cannot access the property value anymore.您无法再访问该属性值。 - The getVolume() method comes into play here. - getVolume()方法在这里发挥作用。 This method is called a getter and is solely created to get the value of the its related property by returning the property - not directly by accessing it.此方法称为getter ,仅用于通过返回属性来获取其相关属性的值,而不是直接通过访问它。
  • You cannot change the property value anymore.您不能再更改属性值。 - A setVolume(value) method (which is not present in your code) comes into play here. - setVolume(value)方法(在您的代码中不存在)在这里发挥作用。 This method is called a setter and is solely created to change the value of its related property through a parameter - again, not directly.此方法称为setter ,仅用于通过参数更改其相关属性的值 - 同样,不是直接。

I think this is simple reason In Tight coupling code you may get the weakness: You are too dependent on the volume variable.我认为这是一个简单的原因在紧耦合代码中你可能会遇到弱点:你太依赖于体积变量。 For example if there a change reference field from volume to another, you must also change it in the calling function例如,如果将参考字段从卷更改为另一个,您还必须在调用 function 中更改它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM