INTRODUCTION
This article will is all about a design concept called “program to interface not implementation”. We could have heard question why "Always program to Interface not to the implementation" in many of the technical discussions.
Here I have tried to explain as clear as possible. It is basically helps to avoid tight coupling on the code.
USECASE
Reading data from various data sources.
IMPLEMENTATION - Program to Implementation
Consider your business requirement to read data from a flat file from the server and use the data for the further analysis.
The Initial Implementation might be below:
Create a program for reading data from flat file.
package com.jbr.generalconcepts.prog2impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FlatFileDataReader {
private StringBuilder builder;
private Scanner scanner;
public FlatFileDataReader(String fileName) throws FileNotFoundException {
scanner = new Scanner(new File(fileName));
builder = new StringBuilder();
}
public String read() {
while (scanner.hasNext()) {
builder.append(scanner.next());
}
return builder.toString();
}
}
|
Write a client program to test it.
package com.jbr.generalconcepts.prog2impl;
import java.io.FileNotFoundException;
public class DataReaderMain {
private FlatFileDataReader flatFileReader;
private String fileName = "data.txt";
DataReaderMain() throws FileNotFoundException {
flatFileReader = new FlatFileDataReader(fileName);
}
private String getData() throws FileNotFoundException {
return flatFileReader.read();
}
public static void main(String[] args) throws FileNotFoundException {
DataReaderMain client = new DataReaderMain();
System.out.println(client.getData());
}
}
|
In any software application, change in the requirement is common. we can't escape from it.
Now, your client asks that your application should read data from the database as well and use it for analysis.
To accomplish this you have modify your DataReaderMain.java class since it will read only data from a Flat file.
Disadvantage with your current DataReaderMain.java class is tightly coupled with the FlatFileDataReader.java
To accomplish the new requirement with less code change would be done using "Program to Interface rather than Implementation".
IMPLEMENTATION - Program to Interface
Create an interface for reading data.
package com.jbr.generalconcepts.prog2interface;
public interface DataReader {
String readData();
}
|
Create an implementation for Flat file reader.
package com.jbr.generalconcepts.prog2interface;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FlatFileDataReader implements DataReader {
private StringBuilder builder;
private Scanner scanner;
public FlatFileDataReader(String fileName) throws FileNotFoundException {
scanner = new Scanner(new File(fileName));
builder = new StringBuilder();
}
@Override
public String readData() {
while (scanner.hasNext()) {
builder.append(scanner.next());
}
return builder.toString();
}
}
|
Create an implementation for database data reader.
package com.jbr.generalconcepts.prog2interface;
public class DataDataReader implements DataReader {
private StringBuilder builder;
public DataDataReader() throws Exception {
builder = new StringBuilder();
}
@Override
public String readData() {
// Code to read database data
return null;
}
}
|
Create a client program to test it.
package com.jbr.generalconcepts.prog2interface;
public class DataReaderMain {
DataReader fileReader;
DataReaderMain(DataReader fileReader) {
this.fileReader = fileReader;
}
public String getData() {
return fileReader.readData();
}
public static void main(String[] args) throws Exception {
DataReaderMain flatFileData = new DataReaderMain(new FlatFileDataReader("data.txt"));
System.out.println(flatFileData.getData());
DataReaderMain databaseData = new DataReaderMain(new DatabaseDataReader());
System.out.println(databaseData.getData());
}
}
|
Now your new DataReaderMain.java is more flexible and can have only less code change if there are any further business requirement changes.
SUMMARY
- Program to interface will avoid tight coupling between multiple implementations
- Easy to add new implementations.
No comments :
Post a Comment