Tutorials Design Patterns Interface Segregation Principle

Interface Segregation Principle

This article explains the interface segregation principle and its uses. Interface segregation is a design principle that deals with the disadvantages of “fat” interfaces. Interfaces containing methods that are not specific to it are called polluted or fat interfaces.

Clients should not be forced to depend upon interfaces that they don’t use.

Let us take the example of a multi-purpose machine developed by Xerox. This super xerox system could do a wide variety of jobs like printing, scanning, stapling, photocopying, etc. They can be implemented as a IMachine interface and XeroxMachine implementation as shown below.

public interface IMachine {
	public void print();
	public void staple();
	public void scan();
	public void photoCopy();
}

XeroxMachine implementation

public class XeroxMachine implements IMachine {

	@Override
	public void print() {
		System.out.println("Printing Job");
	}

	@Override
	public void staple() {
		System.out.println("Stapling Job");
	}
	
	@Override
	public void scan() {
		System.out.println("Scan Job");
	}

	@Override
	public void photoCopy() {
		System.out.println("Photo Copy");
	}
}

The super machine is developed and working great. Now the company is getting requests for making a machine that do only print function. Management has decided to develop a specialized print machine. Now developers want to write Printer class that implements IMachine interface. Did you notice any problem? Although you need only print function, you have to implement all other methods of IMachine interface. Now IMachine interface is not following interface segregation principle and called fat interface.

We can breakdown the IMachine into multiple special purpose interfaces as shown below.

public interface IPrinter {
	public void print();
}

public interface IScanner {
	public void fax();
}

public interface IStapler {
	public void staple();
}

public interface IPhotoCopier {
	public void photoCopy();
}

Notes:

What if you are dealing with an existing legacy application where the interface is a fat interface? How do deal with such a situation? Well, you can always use an adapter design pattern for segregation.

Interface segregation brings implementation simplicity and makes easy to debug and maintain.

Interface segregation principle is easy to understand and sounds great in theory, but often difficult to identify the distinct interfaces and it can also end up with the proliferation of interfaces.

Total
0
Shares
guest
0 Comments
Inline Feedbacks
View all comments
Previous Post

How to Use Picasso Image Loader Library in Android

Next Post

Creating a Splash Screen in Android Using Xamarin

Related Posts

State Design Pattern in Java

Over the course of this article, we will examine State design pattern in java with help of realtime examples. The State design pattern belongs to the behavioral family of pattern that deals with the runtime object behavior based on the current state. The definition of State Design Pattern as per the original Gang of Four book is; "Allows an object to alter its behavior when its internal state changes. The object will appear to change its class".
Read more
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
0
Would love your thoughts, please comment.x
()
x