ISP-Interface Segregation Principle

C# Design Patterns and SOLID Principles SOLID Principles With Code Sample
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

Okay, so up to this point we are done with three principles, single responsible principle, open closed principle, and liskov substitution principle. Now it's a time for moving ahead to the fourth one, which is interface segregation principle. And to understand this principle, we just have to add one interface first. So let me take one scenario. If I go back to my Visual Studio, inside my Visual Studio, I'm going to add a new interface now. So I'm going to add a new item and this is going to be an interface and we are giving the name of this interface, I invoice spring.

Now, as per the naming C sharp, it's a good habit that you always add one eye before the name of the interface. So I'm following the same naming convention. And we have an invoice printer which is going to be my public interface. Like always whenever we have an interface we are going to have some of the method signatures in that I hope you all know that interface is going to be an abstraction so you cannot have a meta logic inside that. So assume that we have one method which is having a name print, which is taking one parameter of type invoice and this invoice parameter name we are giving invoice only. So this is my one method which is returning nothing, that's why it's void and assume that we have one more method.

So, we are just changing the name of this methods properly. So this method name is print and name of this method is going to be print with the logo. So, we have two different methods assume that some of our clients are looking for a print method and some wants to print an invoice with a logo actually. So we have two different methods inside this interface. Now, if I want to use this interface for a printing method and all this thing, then we need to take one particular class which can implement this interface. So assume that we have a new item, which is going to be a class.

And this class is going to maybe having a name bring thing system. So this printing system class is going to allow me to print as I like. So, I'm going to implement the interface I invoice printer inside that. And now whenever I want to use an interface, I have to right click on this and I have two quick action in these options which is implement interface. Now when I click on implement interface, we know that is going to give me all the methods of that interface. And I need to write a logic inside all of these methods.

Now assume that if I have a client who just want to take a print method and they do not want to take print with logo or in the reverse case They want to take print with logo they do not want to take print method. In this case, my interface is forcing me to use both the methods I cannot take only one. For example, if I remove this one, it's going to start showing me an error inside this that printing system does not implement the interface, which is going to have a method with the name print with logo. This is something which is my principle number four, and if you focus on interface segregation principle saying that this one liner for this is any interface should not force any method to a class which is implementing it. So, they are saying that any interface should not force any method like in our case my I invoice print interface is actually forcing both the methods inside this.

Now, if I did not want that my interface should not force then I should have only limited number of methods inside there which are required. For example, my invoice print interface is having print and print with logic. Now if some of the clients wants to use only one method, it's not possible in this case because it's going to force both. The solution of this thing is I need to remove this method from this. So I'm going to do Ctrl x. And now I invoice print is only having one method which is print, which is implementing here.

Now I'm going to add one more interface. So I'm going to add I add new item. And this is going to be an interface. Let's say the name of the interface we are giving I invoice sorry, they're giving I print with logo. So they are giving I print with logo. That's the new interface which we are adding and this interface is going to have a method which is for print with logo.

So now we have two different interfaces I invoice print, and we have I print with logo. Now both of these interfaces are actually having one one method. So inside In any particular class, if I want to use only one method, I can implement the interface and I can use that. While it's also possible that suppose I have a new client so I'm adding a new class. Suppose this is my printing printing system for let's say client two. So in this new class, which is printing system for client two, we are assuming that like printing system is only looking for one letter print which is coming from I invoice print.

Maybe I have a client, they want to take a normal print as well as they want to utilize the new functionality which is printing invoices with logo. Now if they want to utilize both, the only thing is because interface support multiple inheritance, we can specify that this class is going to first take I invoice print So from that we can get a normal print method. And then we can even implement one more interface inside this, which is I print with logo. And the moment we implement this interface, we'll get the second method. So this time is also following interface segregation principle, because this interface is only giving me one method and the other interface if you need another method, and none of the interfaces are forcing any extra methods into my class. If any particular client wants to use only one, they can use one.

And if somebody wants both the functionality, we can take the help of multiple inheritance and we can take both interfaces into that particular class. And we can write the logic. So we can have a print logic which is a simple print logic inside this. And same way we can have an additional print logic which is going to be a print logic with logo. And when we do this thing, it's going to have both logics inside printing system client to while printing system is just going to have One particular logic for normal printing. Now, in this case, we are following interface segregation principle, which is telling us that any particular interface should not force any extra methods into the class.

And that's why we are keeping one one interface with only one simple method inside that. Sometime even people say this thing that interface segregation principle is very much similar to single responsibility principle. The only difference is single responsibility principle talks about a focus purpose for a particular class, while in this case, interface segregation principle talks about one single focus purpose with one single method inside interface. I hope you're getting what I'm saying. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.