Thoroughly understand the class design principle - single responsibility principle

original
2022/01/05 15:02
Number of readings 265

Class design principle - single responsibility

The single responsibility principle of class design is one of the most commonly used principles of class design

English: SRP, Simple Responsibility Principle

Chinese: Single responsibility principle

This is object-oriented Class Design The first principle in is also the simplest one, but in fact it is far from so simple. Many people may not really understand it!

Class design, generally speaking, is not how to write a class, but what principles should be followed to design a class.

Let's look for some online explanations: see how the masters or classic websites explain.

Baidu Encyclopedia: As far as a class is concerned, there should be only one reason for its change. There should be only one responsibility.

To be honest, I didn't understand this sentence at first, because there are too many reasons for a class change, such as

  • Adding a method to a class is a change, right?
  • Is adding an attribute to a class a change?
  • Is adding a function to the method of a class a change?
  • ......

There are too many reasons for these changes. If each reason is a responsibility, SRP simply cannot judge!

Wikipedia: (The content is basically the same as that of Baidu Encyclopedia). In general, one class only does one thing

This explanation is easier to understand and more consistent with the understanding of Chinese people. But when you think about it carefully, there are still several things that are difficult to understand:

What is "one thing"?

for instance:

For example, there is a student management class, which includes Add student information , Modify student information Query student information Delete student information

Here comes the question. Are these four things? Or one thing?

It seems that there are four things, but people with a little experience know that these four things are implemented by one class instead of designing four classes!

So the key is: what is "one thing"? Is it one thing for each function?

In fact, the answer lies in ourselves, because as long as we work, we are always undertaking certain duty

Now let's look at our own duty

  1. For example, as a programmer, my responsibilities are Writer , but Writer There are many things, such as code unit testing System test Bug fix attend a meeting Write Document etc.
  2. For example, my boss is a manager, and his responsibilities are Management programmer He also has many jobs, such as Plan , team building attend a meeting , coordinate performance appraisal etc.
  3. For example, I am a courier, and my responsibility is to deliver goods, but I also have many things to do, such as to subcontract , express , Collection , attend a meeting etc.

These responsibilities are not defined by ourselves, but by the company or department or organization when we arrange work.

in other words, duty It is defined from the perspective of others, not by ourselves.

After we duty By analyzing the definition, we can draw two important conclusions about responsibilities

  • Responsibilities are defined from the perspective of others
  • Responsibility is not one thing, but many things, but these things are closely combined with responsibility.

In the field of object-oriented design, we can say that the responsibilities of a class should be defined as follows:

  • Class responsibilities are defined from the perspective of other classes
  • Class responsibilities include multiple related functions

Therefore, SRP It can be translated as A class is only responsible for a group of related matters , corresponding to the code: A class has multiple methods, which are related

With this definition, let's take a look Student information management , obviously, its four functions are Administration Relevant, according to SRP Only one should be designed Student information management That's all right.

Welcome to our HelloWorld developer community, www.helloworld.net , The domain name is easy to remember

Application scope of SRP

But the real world is often more complex than the ideal. A typical example is Office integration

According to SRP, printers can be designed as a class, photocopiers can be designed as a class, scanners can be designed as a class, and fax machines can also be designed as a class

But just one Office integration , this machine integrates Print to copy scanning , Fax 4 responsibilities!

If we design a Office integration It is impossible to design a class that conforms to SRP Office integration class

What should I do? Is SRP incorrect? Or should we never design one Office integration Class of?

In fact, SRP is not wrong, Office integration It should also be designed, but not constrained by SRP principles Office integration Such a class!

In other words, SRP actually has a scope of application. SRP is only suitable for those basic classes, but not for building aggregate classes of complex classes based on the basic classes

stay Office integration In the example of, printer , Copier , Scanner fax machine All are basic classes, and each class has a responsibility

and Office integration It is an aggregation class and integrates 4 functions at the same time!

Careful you may find that SRP cannot be applied to aggregate classes, so how to ensure the design quality of aggregate classes?

In other words, how to design such an aggregation class in such a situation?

This question has a detailed answer in GoF's book Design Patterns, that is, object composition is preferred to class inheritance.

That's all for the single principle of class. Here's a summary:

  • Single principle of class (SRP): a class is responsible for only one set of related matters, which corresponds to the code: A class has multiple methods, which are related
  • Responsibilities are defined from the perspective of others
  • Class responsibilities include multiple related functions
  • SRP is only suitable for those basic classes, but not for building aggregate classes of complex classes based on the basic classes
  • For complex aggregate classes, composition is preferred over inheritance
  • Finally, welcome to follow our HelloWorld Developer Community , URL: www.helloworld.net The domain name is easy to remember

Welcome to our next article to explain more principles of class design.

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top