What is Interface Segregation Principle (ISP)
The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule.
The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one sub module.
For example,
We have an interface called IDataAccess.
interface IDataAccess
{
public Customer GetCustomer(Guid CustomerID);
public void SetCustomer(Customer customer);
public Customer GetCustomerForReport(Guid CustomerID);
}
The above interface is polluted by having the third method declaration when a report data access class implements this. The report class needs only the GetCustomerForReport method but for the other two methods it will have implement something, atleast it will throw NotImplemented exception for the first method.
So, in this case we have to segregate this interface into two,
interface IDataAccess : IReportDataAccess
{
public Customer GetCustomer(Guid CustomerID);
public void SetCustomer(Customer customer);}
interface IReportDataAccess
{
public Customer GetCustomerForReport(Guid CustomerID);
}
With the above interfaces, the existing code will work because the IDataAccess is inheriting from IReportDataAccess and all your report classes can just implement the IReportDataAccess interface.