首頁 > 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學習者快速成長!