Posts Tagged ‘interface’

Coding to Interfaces

December 18, 2010

Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.

Anytime if you are writing code that interacts with subclasses, you have two choices either; your code directly interacts with subclasses or interacts with interface. Like this situation, you should always favor/prefer to coding to an interface, not the implementation.

I have created a simple example with JDK classes & interface which demonstrates its benefits.

CodingToAnInterface.java

 import java.util.Hashtable;
 import java.util.LinkedHashMap;
 import java.util.Map;

 public class CodingToAnInterface {

 public static void main(String[] args) {
 // Coding to a class : Creating new instance of Hashtable & Assign it to the class.
 Hashtable subject = new Hashtable();
 CodingToClass c2c = new CodingToClass();
 subject = c2c.addSubject(subject);
 System.out.println("CodingToClass : Hashtable : subject : "+subject);

 // Coding to an interface : Creating new instance of Hashtable & Assign it to the Map interface object.
 Map subj = new Hashtable();
 CodingToIntf c2i = new CodingToIntf();
 subj = c2i.addSubject(subj);
 System.out.println("CodingToInterface : Instantiated Hashtable : subject : "+subj);

 // Now, we have a requirement to show subjects in the order it was inserted. Let’s make a simple change from Hashtable to LinkedHashMap.
 subj = new LinkedHashMap();
 subj = c2i.addSubject(subj);
 System.out.println("CodingToInterface : Instantiated LinkedHashMap : subject : "+subj);
 }
 }

 class CodingToClass{
 // Method argument & return type are defined with class name Hashtable. This allows only Hashtable class as parameter.
 public Hashtable addSubject(Hashtable subject) {
 subject.put("Subject1", 50);
 subject.put("Subject2", 70);
 subject.put("Subject3", 60);
 subject.put("Subject4", 80);
 return subject;
 }
 }

 class CodingToIntf{
 // Method argument & return type are defined with interface Map. This allows to send various possible interface subclasses.
 public Map addSubject(Map subject) {
 subject.put("Subject1", 50);
 subject.put("Subject2", 70);
 subject.put("Subject3", 60);
 subject.put("Subject4", 80);
 return subject;
 }
 }
 

Output

CodingToClass : Hashtable : subject : {Subject4=80, Subject3=60, Subject2=70, Subject1=50}
CodingToInterface : Instantiated Hashtable : subject : {Subject4=80, Subject3=60, Subject2=70, Subject1=50}
CodingToInterface : Instantiated LinkedHashMap : subject : {Subject1=50, Subject2=70, Subject3=60, Subject4=80}

In the above example, initially assigned Hashtable object to interface Map object instead of Hashtable class. Later, we have a requirement to show the subjects in the order we have inserted into it. So we need to change the code and this can be easily achieved here by simply changed the instantiation part from Hashtable to LinkedHashMap without touching addSubject part.
However in coding to class, we have to change all the places where Hashtable is being used.

Instead of your code being able to work with only specific subclass called Hashtable, you are able to work with more generic Map interface. In future, it’s easier to change into some other sub class of Map interface.

Advantages

  • App/Software is easier to extend
  • Adds Flexibility to your App.
  • Helps to maintain the loose coupling of code.
Advertisements