Object-oriented: single task principle (SRP)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.