Sobes.tech
Senior

What criterion do you use to decide on interface splitting? How to handle a class with 20 public methods?

sobes.tech AI

Answer from AI

The decision to split an interface is usually based on the Interface Segregation Principle (ISP) from SOLID. If an interface is too large and contains methods that are not always needed by all clients, it should be split into several more specialized interfaces. This increases modularity, simplifies testing and maintenance.

If a class has 20 public methods, it may be a sign that the class is overloaded with responsibilities. It is recommended to:

  • Analyze the methods and group them by meaning.
  • Create separate interfaces for each functional group.
  • Split the class into several classes with fewer methods, each implementing the corresponding interface.

Example:

interface UserAuthentication {
    void login();
    void logout();
}

interface UserProfileManagement {
    void updateProfile();
    void changePassword();
}

class UserService implements UserAuthentication, UserProfileManagement {
    // Implementation of methods
}

This approach improves code readability and maintainability.

What criterion do you use to decide on interface… - sobes.tech