SOLID Principles : Guidelines for software development :: ISP
The SOLID principles provide five guidelines that, when followed, can dramatically enhance the maintainability of software.
- Interface Segregation Principle Intro
- Download Examples
The ISP specifies that clients should not be forced to depend upon interfaces that they do not use. Instead, those interfaces should be minimized.
Intent
Clients should not be forced to depend upon interfaces that they don’t use.
The Principle
Some classes have public interfaces that are not cohesive.
- They may include several groups of members where each group is used by a different set of client classes.
- The groups may be entirely separate or there may be overlap between the members used by different clients.
- Ideally all classes would have cohesive interfaces. Unfortunately, this is not always possible.
The Interface Segregation Principle (ISP) states that clients should not be forced to depend upon interfaces that they do not use. When we have non-cohesive interfaces, the ISP guides us to create multiple, smaller, cohesive interfaces. The original class implements each such interface. Client code can then refer to the class using the smaller interface without knowing that other members exist.
When you apply the ISP, class and their dependencies communicate using tightly-focussed interfaces, minimising dependencies on unused members and reducing coupling accordingly.
Smaller interfaces are easier to implement, improving flexibility and the possibility of reuse.
As fewer classes share interfaces, the number of changes that are required in response to an interface modification is lowered. This increases robustness.
Download Examples
Code Available Before ISP
Contact.java
package com.javaskool.before;
public class Contact {
public String Name;// { accessor; mutator; }
public String Address;// { accessor; mutator; }
public String EmailAddress;// { accessor; mutator; }
public String Telephone;// { accessor; mutator; }
}
Dialler.java
package com.javaskool.before;
public class Dialler {
public void MakeCall(Contact contact)
{
// Code to dial telephone number of contact
}
}
Emailer.java
package com.javaskool.before;
public class Emailer {
public void SendMessage(Contact contact, String subject, String body)
{
// Code to send email, using contact's email address and name
}
}
Code Available After ISP
IDiallable.java
package com.javaskool.after;
public interface IDiallable {
String Telephone="";// { accessor; mutator; }
}
IEmailable.java
package com.javaskool.after;
public interface IEmailable {
String Name="";// { accessor; mutator; }
String EmailAddress="";// { accessor; mutator; }
}
MobileEngineer.java
package com.javaskool.after;
public class MobileEngineer implements IDiallable{
public String Name;// { accessor; mutator; }
public String Vehicle;// { accessor; mutator; }
public String Telephone;// { accessor; mutator; }
}
Contact.java
package com.javaskool.after;
public class Contact implements IDiallable,IEmailable{
public String Name;// { accessor; mutator; }
public String Address;// { accessor; mutator; }
public String EmailAddress;// { accessor; mutator; }
public String Telephone;// { accessor; mutator; }
}
Dialler.java
package com.javaskool.after;
public class Dialler {
public void MakeCall(IDiallable target)
{
// Code to dial telephone number of target
}
}
Emailer.java
package com.javaskool.after;
public class Emailer {
public void SendMessage(IEmailable target, String subject, String body)
{
// Code to send email, using target's email address and name
}
}
Recent Comments