[Object-oriented] the single Responsibility Principle

Source: Internet
Author: User

SRP: thesingle Responsibility Principle

Assignment principle

None but Buddha himself must take theresponsibility of giving out occult secrets...

-E. Cobham Brewer1810-1897.

Dictionaryof phrase and fable. 1898.

(Note: it was difficult to translate this sentence at the beginning. I understood it as "Even Buddha has his responsibilities ")

 

The SRP principles are tomdemarco and
Meilir page-Jones proposed at work. They call this principle"Cohesion". In Chapter 21, we will provide more definitions at the package level. This SRP principle is for class layers.

 

SRP: Principle of employment

The reason for modifying a class should not exceed one.

Take the bowling game in Chapter 6 as an example. Game has two completely independent roles. It keeps track of each frame and performs score calculation. Finally, it is divided into RCM and RSK classes. The role of the game class to keep track of frames and calculate scores.

Why are these two functions divided into two classes? This is because these two functions are modified on their own. We only need to modify the corresponding class to modify the corresponding function. If a class has more than one function, we will modify it for multiple reasons ).

Example 9-1. The rectangle class has two methods. One is to render the rectangle to the screen, and the other is to calculate the area of the rectangle.

 

More than one function

Two different programs use the rectangle class. A program isGeometric Operation ProgramYou need to use the rectangle class to perform mathematical operations on the ry. It does not need to output the rectangle to the screen. Another program isImage display programIt also needs to output the rectangle to the screen while requiring geometric operations.

This design violates the SRP. The rectangle has two responsibilities: computing area and Gui-related rendering.

Violating SRP may cause some problems.

First, such a rectangle class must contain Gui-related codeGeometric Operation Program. If it is a C ++ program, you need to link the relevant GUI library to increase the overhead of complie and link memory. If it is a Java program, it also needs to rely on the GUI library of the relevant platform.

Second, ifImage display programTo modify the rectangle classGeometric Operation ProgramIf you forget to do this stepGeometric Operation ProgramThe program will crash at any time due to the offset of the function pointer.

A better design is to divide functions into 9-2. Divide rendering and area calculation into different classes so that the two programs are independent.

 

What is a function?

 

Previously, we defined SRP as a functional function that changes for "one reason ". If you have more than one motivation to change a class, this class actually has multiple functions. In many cases, this situation is very difficult to find. We are used to putting a logic into a function. For example, the modem interface in Table 9-1 below. Most people think it is reasonable. Four modem functions are declared by four functions.

 

However, it implies two functions. The dial and hangup functions belong to communication management, and the send and Recv functions belong to data transmission.

Should these two functions be separated? In most cases, they should be separated. These two groups of functions have almost nothing in common. They must be modified for different reasons. In addition, these two groups of functions are also called by different program modules. These differences also lead to the differences in the reasons for modification.

Therefore, the design of Figure 9-3 is reasonable. Only two interfaces can be separated. Finally, the client connects the two interfaces.

However, you will find that I have added two more functions to the modemimplementation class. Although unpleasant, This is a must. We often couple some things for some reasons, such as hardware or operating systems. However, if we separate them into different interfaces, We need to decouple the rest.

We can regard the modemimplementation class as a assembly machine. Note that all dependencies will disappear from it. Because all other classes do not depend on it. Except the main function, other classes do not need to know its existence. Therefore, we put these ugly implementations into modemimplementation, and the ugly implementations will not pollute other parts of the program.

 

Conclusion

SRP is the simplest principle, but it is also the most difficult to do well. We naturally connect our responsibilities. The so-called software design is to find and separate responsibilities. The rest of the principles we will discuss will return to the SRP point in one way or another.

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.