You are on page 1of 2

Interface Segregation Principle

The Interface Segregation Principle (ISP) is a principle of object-oriented design that emphasizes the importance of designing interfaces
that are focused and cohesive. The ISP states that clients should not be forced to depend on interfaces they do not use. This means that
classes should not be forced to implement methods they do not need or use.

We saw an example of this in previous example of Liskov Substitution Principle (LSP) where RDAccount had to implement withdraw
method which it did not require. We solved it by extracting withdraw method of Account into WithdrawableAccount.

Why is this important:

1. Enhances modularity: By adhering to the ISP, interfaces are designed to be more focused and cohesive. This makes it easier to
understand, modify, and maintain code since each interface is responsible for a specific set of behaviors.
2. Facilitates code reuse: When interfaces are designed to be more focused, it becomes easier to reuse code in other contexts. Clients
can choose to implement only the interfaces that they need, and can avoid depending on methods they do not use. This makes it easier
to use existing code in new projects, which can save time and effort in development.

3. Reduces coupling: By adhering to the ISP, dependencies between components can be reduced. This is because clients only depend
on the interfaces that they use, and not on methods they do not use. This can make it easier to change components without affecting
other parts of the system.
4. Improves maintainability: By adhering to the ISP, code is designed to be more modular and easier to understand. This can make it
easier to maintain the code over time since changes can be made to specific interfaces without affecting the rest of the system.
5. Increases testability: By designing interfaces that are more focused and cohesive, it becomes easier to test components in isolation.
Clients can test only the methods that they use, and can avoid testing methods they do not use. This can make it easier to test
components and ensure that they work correctly.

Overall, the Interface Segregation Principle is an important principle of object-oriented design that helps to create more modular, flexible,
and maintainable code. By designing interfaces that are focused and cohesive, developers can create code that is easier to understand and
modify, and that is less prone to errors and bugs.

Another option to solveissue RDAccount would have been to break Account into WithdrawableAccount with method withdraw and
DepositableAccount with method deposit . Following would have been the class definition of accounts.

1 public interface DepositableAccount{


2 public double deposit (double deposit);
3 }
4
5 public interface WithdrawableAccount {
6 public double withdraw(double withdrawal);
7 }
8
9 public class SavingAccount implements WithdrawableAccount, DepositableAccount {
10
11 public class CurrentAccount implements WithdrawableAccount, DepositableAccount {
12
13 public class RDAccount implements DepositableAccount{

We did not chose this option as we do not yet have an account where deposit is not an option.

Code Smells

Some typical smells that a class is not following the ISP include:

1. The class implements an interface that includes methods it does not need or use.
2. The class has methods that are not part of any interface, making it difficult to reuse the class in other contexts.
3. The class has a large number of methods, making it difficult to understand and maintain.
4. The class requires clients to implement methods they do not need or use.
5. The class requires clients to depend on methods they do not need or use.

These smells can lead to code that is more difficult to understand, modify, and maintain. They can also make it more difficult to reuse code
in other contexts, since clients may be forced to depend on methods they do not need or use. By adhering to the Interface Segregation
Principle, developers can create code that is more modular, flexible, and maintainable, and that is easier to reuse and extend over time.

You might also like