The Single Responsibility Principle is abbreviated as SRP. A class or module should have A single responsibility. A class or module is responsible for only one responsibility (or function).

The definition of the single responsibility principle is very simple: a class is responsible for performing only one responsibility or function. That is, don’t design large, comprehensive classes, but small, single-purpose classes. On the other hand, if a class contains two or more functions that are unrelated to the business, it is said that its responsibility is not single enough and should be split into more single-function, fine-grained classes.

How do YOU determine if a class has a single responsibility

In fact, we do not have a very clear and quantifiable standard to evaluate whether the responsibilities of a class are simple enough, so to speak, it is a very subjective matter. In fact, in real software development, we don’t need to be too proactive and over-design. So, we can start by writing a coarse-grained class that meets our business needs. As the business grows, if the coarse-grained class gets bigger and bigger, and the code gets more and more, then we can split the coarse-grained class into several more fine-grained classes. This is called continuous refactoring.

In different application scenarios, different business levels, and different stage requirements, the determination of whether the responsibility of the same class is single may be different. A class design may satisfy the single responsibility principle in one application scenario or current requirements, but it may not satisfy the single responsibility principle in another application scenario or future requirements, and needs to be further broken down into more granular classes.

For example, in a social product, we use the following UserInfo class to record user information.

public class UserInfo {
  private Long userId;
  private String username;
  private String email;
  private String avatarUrl;
  / / province
  private String provinceAddress;
  / / the city
  private String cityAddress;
  / / area
  private String regionAddress;
  // Full address
  private String detailAddress;
}
Copy the code

If the UserInfo class is designed to contain information that is purely for display, like our Tiktok, then Userlnfo is now designed to meet the single responsibility principle. However, if the social product is later added to the e-commerce module, and the user’s address information is also used in e-commerce logistics, the design of the UserInfo class does not meet the principle of a single responsibility, then it is best to separate the address information from Userlnfo and separate it into a dedicated user address information.

So how can we tell if a class has a single responsibility in real development? Here, we can judge whether a class is sufficiently single from some side indicators. For example, the following situations may indicate that the design of a class does not meet the single responsibility principle:

  • Too many lines of code, functions or attributes in a class will affect the readability and maintainability of the code, so we need to consider splitting the class.

  • The class depends on too many other classes, or too many other classes depend on the class, which does not conform to the design idea of high cohesion and low coupling, we need to consider the separation of the class;

  • There are too many private methods, so we should consider whether private methods can be separated into new classes and set as public methods, which can be used by more classes, so as to improve code reuse.

  • It is difficult to give a class a proper name, it is difficult to generalize into a business noun, or it can only be named with some general Manager Context, which means that the responsibilities of the class may not be clearly defined.

  • A large number of methods in a class operate on a few properties of the class, so consider separating those properties from the corresponding methods.

Is the responsibility of a class as simple as possible

The single responsibility principle improves class cohesion by avoiding the design of large, all-in-one classes and coupling unrelated functions together. At the same time, the class responsibility is single, the class depends on and depends on other classes will be less, reduce the code coupling, in order to achieve high cohesion, low coupling code. However, if you break it down too much, it can actually do the opposite, reducing cohesion and the maintainability of your code. Therefore, when designing a class, just meet the current requirements, not over design.