Loose Coupling Example Using Interface

Coupling is the degree to which one class knows about another class. If the only knowledge that class A has about class B is what class B has exposed through its interface, then class A and class B are said to be loosly coupled….that’s a good thing. If, on the other hand, class A relies on parts of class B that are not part of class B’s interface ,then the coupling between the classes is tighter….not a good thing. In other words, if A knows more than it should about the way in which B was implemented, then A & B are tightly coupled.

Loose coupling is the desirable state of having classes that are well encapsulated, minimise references to each other, vand limit the breadth of API usage. While, Tight coupling is the undesirable state of having classes that break the rules of loose coupling.

On the other hand cohesion is all about how a single class is desiged. The term cohesion is used to indicate the degree to which a class has a single, well focused purpose. The more focused a class is, the higher its cohesivenes….a good thing.

 

Loose coupling has advantage over tight coupling.

Lets understand it with example

In below example we have two classes A and B

Class B implements Interface i.e. InterfaceClass.

InterfaceClass defines a Contract for B class as InterfaceClass have abstract methods of B class that can be access by any other class for example A.

In Class A we have display method which can accept object of class which implements InterfaceClass (in our case it is B class). And on that object method of class A is calling display() and getVar() of class B

In MainClass we have created object of Class A and B. And calling display method of A by passing object of B class i.e. objb. Display method of A will be called with object of B class.

Now talking about loose coupling. Suppose in future you have to change the name of Class B to ABC then you do not have to change its name in display method of class B, just make the object of new (ABC class) and pass it to the display method in MailClass. You do not have to change anything in Class A

—A.java—

package interface_package.loose_coupling;

public class A {

	void display(InterfaceClass obji)
	{
		obji.display();
		System.out.println(obji.getVar());
	}
}

—B.java—

package interface_package.loose_coupling;

public class B implements InterfaceClass{

	private String var="variable Interface";
	
	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("Display Method Called");
	}
}

—InterfaceClass—

package interface_package.loose_coupling;

public interface InterfaceClass {

	void display();
	String getVar();
}

—Main.class—

package interface_package.loose_coupling;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		A obja=new A();
		B objb=new B();
		obja.display(objb);		//Calling display of A class with object of B class 
		
	}
}

 

OUTPUT
loose coupling

 

Download Project from GitHub

%d bloggers like this: