>

Thursday, 17 December 2015

Interface Segregation Principle

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use.
ISP violation leads to tight coupling.
The larger a component gets the larger its interface will get. This can make it harder to understand the purpose of a component, but it can also cause increase coupling, where by components that make use of such a component are exposed to more of that components capabilities that are appropriate. The Interface Segregation Principle (or ISP) aims to tackle this problem by breaking a components interface into functionally separate sub-interfaces. Although a component may still end up with the same set of public members, those members will be separated into separate interfaces such that a calling component can operate on the component by referring only to the interface that concerns the calling component.
That way calling components will not be overwhelmed by irrelevant operations that have been separated out into other interfaces.
Violation of ISP:
interface Ivehicle {
public void run();
public void Applygear();
}
class Car implements Ivehicle {
public void run() {
// ....running
}
public void Applygear() {
// ...... apply gear
}
}
class bus implements Ivehicle {
public void run() {
//.... running
}
public void Applygear() {
//.... apply gear }
}
class Manager {
Ivehicle vehicle;
public void setvehicle(Ivehicle v) {
vehicle=v;
}
public void manage() {
worker.run();
}
}
Here Ivehicle interfaces in implemented by car and bus.This interface contains 2 methods run() and Applygear() and the concrete classes car and bus implement these two methods.If I add a automatic vehicle with out gears to the list of vehicles,it is forced to implement the Applygear() method which does n’t make sense.To tackle this problem,the interface Ivehicle should be split into two interfaces,Ivehicle and IApplyGear.

0 comments:

Post a Comment