[General boards] [Winter 2019 courses] [Fall 2018 courses] [Summer 2018 courses] [Older or newer terms]

Interface Segregation vs Single Responsibility


#1

Often times, it seems like the interface segregation principle and single responsibility are talking about the same thing, one just extends to include interfaces while one is for classes, what would be the way to distinguish this on the final when asked to analyze solid principles for example?

For example, in the practice question bank, you could say that since Ticket implements a static numSales, it’s interface segregation since a Ticket should not implement that since its a class that gets extended into other multiple types of tickets, instead it should be the job of a TicketManager to keep track the number of all different types of tickets. However, wouldn’t that fall into the single responsibility umbrella too?


#2

Interface Segregation Principle refers to methods inside the interface, whereas Single Responsibility Principle refers to methods inside a class.

The former says not to do this:

interface DrawAndAccessible{
. void draw();
. void access();
. int accessHelper();
}

It looks like Drawable and Accessible are completely different things. They should be in different interfaces so that no class has to implement both if they only need one.

There is no such thing as a lazy interface. There are, however, lazy classes. So Single Responsibility Principle is about finding the right balance between too many and too few methods.

Interface Segregation Principle is about keeping interfaces cohesive. In theory, these are the same thing: each class and each interface should be responsible for one thing. But in practice, this means keeping classes middle-sized but keeping interfaces as small as possible, while still ensuring that an implementing class will have the necessary methods to have an iterator (for Iterable) or to be drawn on the screen (for Drawable) etc.


#3

So for example on an exam, if the supplementary code implements a completely unrelated interface or there’s an interface with methods with low cohesion, we would say that is violating interface segregation but if a class was trying to do many things at once, we would say that’s Single Responsibility?


#4

“Implements completely unrelated interface” is fine. Serializable and Iterable are unrelated, but might be useful to the same class.

The interface itself would have to have unrelated methods inside it to violate Interface Segregation Principle.

A class with unrelated methods would violate Single Responsibility Principle.