The five principles proposed by Robert C. Martin for the object are described here. Single Responsibility Principle (SRP): the class has only one responsibility, and there cannot be multiple reasons for modifying the class.
Reference Document (http://www.objectmentor.com/resources/articles/srp.pdf)
The class that follows the SRP single responsibility principle is sufficient to meet the following conditions:
◇ Only one role (responsible)
◇ Modify the field combination of classes for only one reason
Why is the responsibility for going to the class single?
Assume that a class has multiple responsibilities and there are more than two reasons for class modification. When a function is modified, other functions are easily affected. The stability and maintenance convenience of the software is very poor.
SRP is defined as "reason for modification = responsibility" in the original statement. Even though the theory is clear, it is more difficult to judge in practice.
The following is an example by phone:
Modem. Java
Interface modem {
Public void dial (string PNO); // call
Public void hangup (); // call
Public void send (char C); // send mail
Public char Recv (); // receive
}
The modem interface defined in this way seems to be a set of functions, essentially two sets of functions.
◇ Connection management (dial and hangup)
◇ Send and receive data (send and Recv)
Why are there two groups of functions?
Assume that the modem has the following changes:
◇ Modify the connection method modem class
◇ Modify the receive modem class of the mail sending Method
Therefore, the corresponding changes are used as two groups of functions.
In this way, we can redefine the following:
Connection Management Interface:
Connection {dial, hangup}
Data sending and receiving interface:
Datachannel {send, Recv}
Then the specific implementation is handed over to the class of the successor interface for implementation.
Generally, the design of the single responsibility principle class of SRP can be implemented through the following steps:
① Search for functions: What kind of functions are there?
(2) Aggregation Function (definition class): similar functions are classified into one class.
③ Reasons for modification: possible reasons for modifying the column name
④ Split class: if the class has multiple reasons for modification, it is divided into multiple classes. Or multiple classes are summarized into one class for only one reason for modification.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hantiannan/archive/2009/09/23/4583691.aspx