Coupling in JAVA is an indicator of a. dependence of a class with the other class in an object-oriented environment, b. level of flexibility the developer has in changing the code across various classes to meet the end user’s requirement, c. the way functionality of a class is used by the other class: directly or with the help of external interfaces, d. the efforts required in maintaining code post-go-live, e. the way innovative software techniques like Inversion of Control and Dependency Injection are used to inject more flexibility in coding and testing of codes.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are two major couplings in Java, and let us analyze them in detail.
In Object-oriented application design, there is always a need to utilize the logic developed in a class in some other class to reuse the efforts already invested and avoid reinventing the wheel.
Direct Collaboration between the classes leads to tight coupling, and its features are
Example of Tight Coupling
Two collaborating classes in this example, “Ordervalue” and “order,” are interdependent. Calling class “Ordervalue” knows the business logic coded in the called class “order” accordingly, the code in the calling class is structured, and any change in the called class will upset the results of the program.
Hence it can be concluded that Classes “Ordervalue” and “Order” are tightly coupled.
Code:
// 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 } }
Output:
In this concept, classes that need to be collaborated to share business logic and common functionalities in OOPS are coupled through external sources. Thus, they are loosely or indirectly connected together.
Major attributes of loose coupling are
Inversion of Control (IOC)
It is a concept by which the control of program modules or objects is transferred to the container framework. This concept is used in OOPS quite regularly. Instead of program codes making calls to a library, the container framework takes over the control and call codes. Dependency is injected into the objects as against objects creating dependencies.
This concept facilitates loose coupling and modularity in programming.
Dependency Injection (DI)
DI is the vehicle through which IOC concepts are put into use, and control transfer takes place in setting up object dependencies.
Examples of Loose Coupling
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:
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.
The above is the detailed content of Coupling in Java. For more information, please follow other related articles on the PHP Chinese website!