ISP - Interface Segregation Principle

Our principles education continues!

Booo to fat! 




 Booo to pollution! 



Wait what is he talking about?

...and shit

Yeah, interfaces. Some like to have interfaces with an humungous number of methods.


The witches?
However those interfaces should be broken into groups of related methods (to increase cohesion).

Another issue is when we add a method to an interface solely for the benefit of one of its subclasses. This method must be implemented or allowed to default in all derived classes. This may also put in jeopardy LSP, leading to maintenance and reusability problems.

Basically, separate clients means separate interfaces. As simple as that :)


ISP - Interface-segregation principle

Clients should not be forced to depend on methods they do not use.

A vehicle interface could contain all kinds of methods: engine related, gear related, steering, radio, etc.
Fat bastard!
By separate them in different interfaces we'll have a simpler system.
Ah, getting better :)

Also, when clients are forced to depend on methods they don't use, those clients are subject to changes on those methods. This results in an inadvertent coupling between all the clients!
So we should break "fat" classes into many client-specific interfaces: the fat class can then inherit all it needs. Other clients may inherit just from the ones they will effectively use.

No comments:

Post a Comment