ISP: Interface Segregation Principle

Aaina jain
Swift India
Published in
2 min readFeb 7, 2019

Convert Fat protocols into small protocols based on group of methods

The interface-segregation principle (ISP) states that

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

When we design an application we should focus on module abstraction which contains several submodules. While creating another module that contains only some of the submodules of the original system, we are forced to implement the full protocols and to write some dummy methods. Such a protocol is named fat protocol.

ISP helps us to avoid:

  • Fat interfaces
  • don’t force classes to implement methods they can’t
  • don’t pollute protocols with lot of methods

The Interface Segregation Principle advocates segregating a “fat interface” into smaller and highly cohesive protocols, known as “role protocols”. Each “role protocols” declares one or more methods for a specific behavior. Thus clients can implement only those “role protocols” whose methods are relevant to them.

Interface Segregation Principle Violation:

When a client depends on methods it doesn’t use, it means that abstractions are wrong.

If ImageMessageView and VideoMessageView implement this protocol then it it will be overhead as image view doesn’t know about video player and vice versa, it needs to implement dummy method to satisfy compile time error. Another solution to make protocol methods optional is create dummy methods in protocol extension and then override required method in a particular class.

Follow Interface Segregation Principle:

By following ISP, We can divide above protocol into 2 parts:

Now VideoMessageView can just conform to DeepLinkVideoMessage. In future if requirement comes to remove video messages we can simply remove this protocol and view without touching other code.

Same for ImageMessageView

Conclusion:

While designing your application think about whether your abstractions are reusable and composable, and whether your objects are encapsulated and cohesive.

If you like the article, Please clap 👏

You can catch me at:

Linkedin: Aaina Jain

Twitter: __aainajain

--

--