Single Responsibility Principle (SRP.
Definition:
There shoshould never be more than one reason for a class to change.
There should be only one reason for the change.
Sometimes, developers may have some problems when designing interfaces. For example, user attributes and user behaviors are declared in an interface. This causes the Business Objects and business logic to be put together. As a result, this interface has two responsibilities and the interface responsibilities are unclear, according to the definition of SRP, it violates the single responsibility principle of the interface.
The following is an example:
Package COM. loulijun. chapter1; public interface itutu {// height void setshengao (double height); double getshengao (); // weight void settizhong (double weight); double gettizhong (); // Boolean chifan (Boolean hungry); // Boolean shangwang (Boolean silly );}
In the above example, the height and weight are business objects, and the corresponding method is mainly responsible for user attributes. While eating and surfing the Internet are the corresponding business logic, mainly responsible for user behavior. However, this will give people a feeling that they do not know what the interface is, their responsibilities are unclear, and various problems will occur during later maintenance.
Solution: the single responsibility principle is to break this interface into two interfaces with different responsibilities.
Itutubo. Java: Responsible for Tutu (painted, for example, Personal Name) attributes
Package COM. loulijun. chapter1;/*** Bo: bussiness object, Business Object * responsible for user attributes * @ author administrator **/public interface itutubo {// height void setshengao (double height ); double getshengao (); // weight void settizhong (double weight); double gettizhong ();}
Itutubl. Java: Responsible for painting
Package COM. loulijun. chapter1;/*** Bl: business logic, business logic * is responsible for user behavior * @ author administrator **/public interface itutubl {// Boolean chifan (Boolean hungry ); // Boolean shangwang (Boolean silly );}
In this way, the single responsibility of the interface is realized. When implementing interfaces, two different classes are required.
Tutubo. Java
package com.loulijun.chapter1;public class TutuBO implements ItutuBO {private double height;private double weight;@Overridepublic double getShengao() {return height;}@Overridepublic double getTizhong() {return weight;}@Overridepublic void setShengao(double height) {this.height = height;}@Overridepublic void setTizhong(double weight) {this.weight = weight;}}
Tutubl. Java
Package COM. loulijun. chapter1; public class tutubl implements itutubl {@ overridepublic Boolean chifan (Boolean hungry) {If (hungry) {system. out. println ("Go eat hot pot... "); Return true;} return false;} @ overridepublic Boolean shangwang (Boolean silly) {If (silly) {system. out. println ("it's boring, it's going online... "); Return true;} return false ;}}
This makes it clear that when you need to modify user attributes, you only need to modify the itutubo interface, which only affects the tutubo class and does not affect other classes.
So what is the significance of the single responsibility principle?
1. Reduce the complexity of the class and clearly define what responsibilities are implemented.
2. improve readability
3. improve maintainability
4. reduces the risks caused by changes and is very helpful for system scalability and maintainability.
However, there is a problem with the use of a single responsibility principle. There is no clear division standard for "responsibilities". If the division of duties is too detailed, it will lead to a sharp increase in the number of interfaces and implementation classes, it increases complexity and reduces the maintainability of the Code. Therefore, we need to analyze the specific situation when using this responsibility. The suggestion is that the interface must adopt the single responsibility principle, and the single responsibility principle should be implemented as much as possible in class design. It is best to cause a class change for one reason.