Detailed Explanation of Six Design Principles (1) - Single Responsibility Principle

Introduction:

Single responsibility principle (SRP: Single responsibility principle), which states that a class should have only one reason for change. The so-called responsibility refers to the reason for the class change. If a class has more than one motivation to be changed, then the class has more than one responsibility. The Single Responsibility Principle states that a class or module should have one and only one reason to change.

Benefits of the Single Responsibility Principle:

  • The complexity of the classes is greatly reduced, and each class is clearly defined.

  • Improve readability.

  • High maintenance.

  • The risk rate is reduced when changing.

 

Converted to Android development, that is, the interfaces, classes, and methods we write should use the single responsibility principle. Let's learn about the single responsibility principle in detail through these three aspects.

 

Interface

 

public interface Weather { //Load the weather of a city on a certain day void LoadWeather(String date,String city); //Get the weather successfully void GetWeatherSuccess(); //Get the weather failed void GetWeatherError(Throwable e); }

From the perspective of the interface itself, there is no problem in writing, but from the perspective of the single responsibility principle, it is problematic. The interface shows two responsibilities, one is to load data, and the other is to prompt information. From another perspective, methods that do not affect each other can be split into multiple interfaces, then let's look at GetWeatehrSuccess() and GetWeatherError(Throwable e). If one of them changes, then the other must change; then LoadWeather( String date, String city) and the other two methods will find that they do not affect each other. So LoadWeather(String date, String city) can be split separately.

 

So the interface should conform to a single responsibility.

method

 

For example, a requirement to modify the user name and password, we write it in a method.

  private void ChangeUser(String username,String address) {        username="abc";        address="天津市";    }

 

The method and responsibilities of the above code are not clear, both the user name and the address are modified, which does not conform to the single responsibility principle. The responsibilities that each method must have must be clear, not only easy to develop, but also easy to maintain.

The correct modification is as follows:

 

   private void ChangeUserName(String username) {        username="abc";    }    private void ChangeAddress(String address) {        address="天津市";    }

kind

 

 

The single responsibility principle of a class is the same as the single responsibility of an interface and a method, but we will find that many classes in project development are not in line with each other. That is because the single responsibility of a class is affected by many factors, such as the project cycle, The level of technicians, etc.

So to sum up, in the development, the defined interface and method must have a single responsibility, and the class depends on the situation.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326049117&siteId=291194637