5. Inferface Segregation Principle (ISP) Flashcards

1
Q

What does ISP state?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What kind of interfaces should we prefer according to ISP?

A

We should prefer small, cohesive interfaces to large, “fat” ones.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the interface of a type in the context of ISP?

A

Whatever can be accessed by client code with an instance of that type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a client in the context of ISP?

A

The code that is interacting with an instance of an interface. It’s the calling code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does violating ISP results in?

A

It results in classes that depend on things they don’t need. This increases coupling and makes it harder to change or swap out individual implementations because the number of clients that might break is much larger.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does having more dependencies than needed cause?

A
  1. More coupling
  2. More brittle code
  3. More difficult testing
  4. More difficult deployments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the three common indicators that ISP is being violated?

A
  1. Large interfaces
  2. NotImplementedException
  3. Code that uses only a subset of a larger interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What can we do to implement ISP when dealing with a large legacy interface that violates it?

A

We can break it into smaller interfaces, and then at places where an error is occurring, we can implement the pieces of the now broken interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does violating ISP negatively impact compliance with LSP?

A

Large interfaces are harder to fully implement, and thus, more likely to only be partially implemented, and therefore, to not be substitutable for their base type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does ISP relate to cohesion?

A

The Interface Segregation principle relies heavily on cohesion — small, cohesive interfaces are preferable to large interfaces whose methods are only loosely related to one another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does ISP relate to PDD?

A

You shouldn’t break up interfaces just to satisfy the principle but rather use the principle to help identify the source of some pain you’re experiencing with the codebase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a potential drawback of not following PDD when implementing ISP?

A

If you try to preemptively follow ISP everywhere, you could end up with a bunch of single-method interfaces that are much more difficult to use, group together, and understand than a few cohesive interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Once identified, how should we fix ISP violations?

A
  1. For interfaces you control:
    Break up the interfaces into smaller ones — if the original interface is still being used, you can compose it from smaller ones for backward compatibility.
  2. For interfaces you don’t control:
    Create small, cohesive interfaces that follow the adapter design pattern — your code should work with the adapter interface that you control and it, in turn, can work with the original large interface where only the adapter knows about the underlying large interface.
  3. Generally:
    It’s easy to follow ISP if you let client code define interfaces the code will work with. In fact, it’s impossible to violate ISP in this case.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can we identify how exactly to implement ISP on a large interface?

A

By looking at a place where the interface is being used, identifying which parts of the interface it uses, and then, if a given part is another interface or method, diving in and seeing what individual parts of that are being used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the key takeaways of this module?

A
  1. Prefer small, cohesive interfaces to large, expensive ones
  2. Following ISP helps with SRP and LSP
  3. Break up large interfaces by using
    a. Interface inheritance
    b. Adapter design pattern
How well did you know this?
1
Not at all
2
3
4
5
Perfectly