OO design principle-single responsibility principle: single responsibility principle of SRP designed by oo

Source: Internet
Author: User

• Overview

There shoshould never be morethan one reason for a class to change.
Never let a class have more than one reason for change.

If a class has multiple reasons to change it, it indicates that the class has multiple responsibilities and it is necessary to redesign the class.

Core meaning of the single responsibility principle of SRP:
◇ A class has only one responsibility.
◇ If a class needs to be changed, there will always be only one reason to change it.

A class must have only one responsibility, which is also the origin of naming a single responsibility principle.

The reason why the design should comply with the single responsibility principle:
If a class has more than one responsibility, there will be multiple reasons for this change, and this change will affect the users with different responsibilities.
◇ If an external class library is used by a role, the user using another role also has to include this unused external class library.
◇ A user needs to modify one of his/her responsibilities for some reason. Another user is also affected and will have to re-compile and configure.

• Division of duties and class Extraction

Robert c Martin gave a famous definition: One of the responsibilities of a class is one of the reasons for this change.
If you can think of more than one motive forchanging a class, then that class has more than one responsibility.
If you can think of multiple reasons for changing a class, this class has multiple responsibilities.

Here we use a modem example to illustrate how to divide duties:

Interfacemodem {public void dial (string PNO); // dialing public voidhangup (); // hanging up public void send (char C); // sending public charrecv (); // receive data}

At first glance, this is an interface design without any problems. But in fact, this interface contains two responsibilities:
◇ The first is connection management (DIAL, hangup
◇ The other is Data Communication (send, Recv)
In many cases, these two roles have no common responsibilities. They are changed for different reasons and called by different programs. Therefore, it violates the SRP principle.

We can find that there are two reasons for the modem class.
◇ Upgrade of the connection method to upgrade of the modem
◇ The receiver of the mail sending method is greater than the receiver of the modem.
Based on these two reasons, we can consider dividing this class into two classes with a single responsibility.

1. responsibilities related to connection management:
Connection {dial, hangup}

2. responsibilities related to data sending:
Datachannel {send, Recv}

 

 

• Conclusion

Single Responsibility Principle (SRP) establishes a benchmark for determining the abstract granularity of classes (interfaces) from the perspective of responsibility (changing reasons): In designing classes (interfaces) for the System) to ensure their single responsibility.

1. A class has only one reason for its change. Otherwise, reconstruction should be considered.

2. SRP is determined by the cause of the change, rather than by the function responsibilities. Although the responsibility is often the axis that causes changes, sometimes it is not necessary to review the situation.

3. Test-driven development helps design the rational separation function.

4. separation of duties can be performed in either the fa c Ade mode or the proxy mode.

 

In general, you can design a class that complies with the single responsibility principle of SRP according to the following:

1. Sorting of functions: What functions should be divided.

2. Class extraction: similar functions can be extracted as a class.

3. Reasons for considering changes: list all reasons for changes that can cause the category.

4. Re-division of classes: If a class has multiple reasons for change, it is divided into classes with a single responsibility. If multiple classes have the same reason for change, you can make these classes into one class.

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.