terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat interface-segregation.md
guest@codeandosimple: ~/blog/solid $ cat interface-segregation.md

SOLID - INTERFACE SEGREGATION PRINCIPLE_

// "You don't have to be great to start. But you have to start to be great" - Zig Ziglar

The Interface Segregation Principle (ISP) states that no client should be forced to depend on methods it does not use.

play_circle

Play explanatory video

In simpler terms, ISP suggests that instead of having a large interface that all clients must implement, it is better to have several small and specific interfaces for each client.

# Segregation and Dependency?

  • Segregation: Dividing large interfaces into smaller and more specific ones.
  • Dependency: Clients should only know the parts they actually need and use.

The Interface Segregation Principle aims to reduce unnecessary dependencies between clients and abstractions, leading to more decoupled systems that are easier to refactor, change, and deploy.

# Why is it important?

Flexibility

Fewer unnecessary dependencies lead to a more flexible and adaptable system.

Maintainability

Modifications are easier since changes in an interface do not affect clients that do not use it.

Clarity

Small and well-defined interfaces are easier to understand and use correctly.

# Symptoms of violation

We can identify when we are not respecting the Interface Segregation Principle:

  • Classes that implement interfaces but only use some of the provided methods.
  • Changes in an interface affecting clients that do not use the modified methods.
  • Difficulty understanding which methods of an interface are actually relevant for a specific client.

Example

Imagine a system to manage workers in a company.

Without Interface Segregation Principle (Fat Interface)

public interface Worker {
    void work();
    void eat();
    void takeBreak();
    // ... other methods
    }

Some workers don't need all these methods (e.g., a RobotWorker doesn't need to eat or take a break), but they are forced to implement them anyway.

With Interface Segregation Principle (Split Interfaces)

We divide Worker into several small and specific interfaces:

public interface Workable {
    void work();
    }
public interface Eatable {
    void eat();
    }
public interface Breakable {
    void takeBreak();
    }

Now, each type of worker only implements the interfaces that correspond to their actual responsibilities.

Conclusions

By implementing the Interface Segregation Principle, we ensure that clients only depend on the interfaces they actually use, which reduces coupling and improves system cohesion. This facilitates changes and improvements and makes the system more understandable and maintainable.

Summary

ISP is crucial for creating flexible and maintainable systems. It promotes the creation of small, specific interfaces that clients can implement without being overwhelmed by unnecessary methods. By following it, we improve clarity and reduce the risk of unwanted side effects from changes.