I of S.O.L.I.D. or Interface Segregation Principle


Next in line in the solid principles is Interface Segregation principle.

The general definition of this principle is

Client should not be forced to implement interfaces they don’t use.

So, this means, any class should be using or implementing only the interface which it needs, for its functionality.

Lets take a real world example. In an organization, each department is having its responsibility, like Development department will work on projects, HR team will be responsible for hiring new members, business development will be responsible for getting new projects for the company and so on. So if we talk about the code which will handle this, it could be something like the following :

1

Now, as we look into the code, we need to see is our HR team need to be concerned about getting projects for company or our business development team need to do the development etc. ? All the concrete classes are required to implement the functions, which are of no use of them(even if its empty).

This is the violation of Interface Segregation Principle.

This type of interface is also called as fat or polluted interface. So to avoid this, we break the responsibilities of each department into their individual tasks and define their own interfaces. So our code will become like the following:

2

So after creating separate interfaces, each department is concerned about its own department, and does not what other departments are up-to. Now, if you even add any new functionality to any interfaces, only the concerned department will be required to implement it.

One important thing to notice here is that, if you closely observe this principle, it seems quite similar to the  Single responsibility principle.

So this was all about the concept of Interface Segregation Principle.

And if you have any thoughts on this, then do share them.

Happy Coding…!!!

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in SOLID Principles and tagged . Bookmark the permalink.

One Response to I of S.O.L.I.D. or Interface Segregation Principle

  1. Bhupinder says:

    good job bro. this is quite similar to strategy pattern which is beautifully explained in head first design patten (book)

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s