首页 > Java > java教程 > 正文

Java中的耦合

WBOY
发布: 2024-08-30 15:14:56
原创
896 人浏览过

JAVA中的耦合是a的指标。 面向对象环境中一个类与另一个类的依赖关系,b。开发人员在更改各种类的代码以满足最终用户的要求时所具有的灵活性,c.另一个类使用一个类的功能的方式:直接或借助外部接口, d. 上线后维护代码所需的努力,e. 使用控制反转和依赖注入等创新软件技术为编码和测试注入更多灵活性的方式代码。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java 中的耦合类型

Java中有两个主要的耦合,让我们详细分析一下。

1.紧耦合

在面向对象的应用程序设计中,总是需要在其他类中利用一个类中开发的逻辑,以重用已经投入的工作,避免重新发明轮子。

类之间直接协作导致紧密耦合,其特点是

  • 被调用类中可用的业务逻辑被创建为调用类内的对象。
  • 由此创建的对象在调用类中执行以实现所需的功能。因此,调用程序知道被调用类中可用的业务逻辑的完整功能。
  • 被调用类中编码的业务逻辑的任何更改都会影响被调用类的结果
  • 如果被调用类中的更改不可避免,则调用类中也必须进行适当的更改。
  • 因此类之间是高度相互依赖的

紧耦合示例

此示例中的两个协作类“Ordervalue”和“order”是相互依赖的。调用类“Ordervalue”相应地知道被调用类“order”中编码的业务逻辑,调用类中的代码是结构化的,被调用类中的任何更改都会扰乱程序的结果。

因此可以得出结论,类“Ordervalue”和“Order”是紧密耦合的。

代码:

// Tight Coupling - Sample program
public class ordervalue // Calling class
{
public static void main(String args[]) // method in the class
{
order b = new order(600,5); // creating object for the called class
System.out.println("value of the order is");
// order and execute it
System.out.println(b.value); // Prints the order value computed by
} // the called class
}
class order // Called class
{
public int value; // method in the class
order(int orderquantity, int rate) // business logic
{
this.value = orderquantity * rate; // computing the value
}
}
登录后复制

输出:

Java中的耦合

2.松耦合

在此概念中,需要协作以共享 OOPS 中的业务逻辑和通用功能的类通过外部源进行耦合。因此,它们松散或间接地连接在一起。

松耦合的主要属性是

  • 类、代码组件和模块之间的依赖性较低。
  • 如果依赖是不可避免的,那么它们通过接口等外部组件进行连接。
  • 使用接口的连接保持最少,以获得松散耦合的好处。
  • 不需要在类中的其他类周围定义对象,并且对象将是独立的。
  • 每个类对这个耦合中的其他类知之甚少。最好的情况是,每个类都会知道其他模块所暴露的接口。
  • 一个类中代码的任何更改都不会影响其他类,并且它们不必更新。
  • 它为开发人员提供了完美的灵活性,可以轻松更改代码并适应新的业务变化。
  • 维护程序的时间和精力大大减少
  • Spring 框架的概念,如控制反转和依赖注入,用于克服紧密耦合。

控制反转 (IOC)

这是一个将程序模块或对象的控制转移到容器框架的概念。这个概念在 OOPS 中经常使用。容器框架接管控制和调用代码,而不是程序代码调用库。依赖关系被注入到对象中,而不是创建依赖关系的对象。

这个概念有利于编程中的松散耦合和模块化。

依赖注入 (DI)

DI 是 IOC 概念得以运用的载体,控制权转移发生在建立对象依赖关系时。

松耦合示例

In the example, three Classes, “month1”, “month2”, “month3” are independent modules, and they collaborate little with each other through an interface “iface”. As a result, these classes have very little knowledge of the other classes on what they are doing. They only know that all the classes are interacting with an interface.

There is no object created using the other classes in any of these classes, and they are the typical examples of loose coupling.

Code:

// Loose Coupling in JAVA - Sample Program
interface Iface //Interface is defined
{
public void monthname(); //module within the interface
}
class month1 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("January");
}
}
class month2 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("Feburary");
}
}
class month3 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("March");
}
}
public class Subject { // Execution starts here
public static void main(String[] args)
{
Iface t = new month1(); // First class called thru
t.monthname(); // interface
Iface tx = new month2(); // Second class called thru
tx.monthname(); // interface
Iface tx2 = new month3(); // Third class called thru
tx2.monthname(); } // interface
}
登录后复制

Output:

Java中的耦合

Conclusion

As far as possible, applications will have to be designed to hold only loose couplings for better maintainability and serviceability and keep interdependence between program components very minimal. However, if interdependence is a must, the components will have to be connected only through the interface.

以上是Java中的耦合的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!