“This is the first day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

preface

At the top end of the interview, often encounter some questions about design patterns, each time the answer is not ideal. Coincides with the more challenging activities in August, I am prepared to spend a month to get a good understanding of design patterns, so as to increase my confidence for the interview.

Before learning about design patterns, it is important to recognize that design patterns are programming ideas that can be applied to any programming language. Secondly, we should learn from the principle of design pattern, so this article will introduce in detail one of the principles of design pattern, the single responsibility principle.

The official definition of

Single Responsibility Principle (SRP)

There should never be more than one reason for a class to change.

There should be one and only one reason for a class to change.

I understand it

Before we understand the single responsibility principle, let’s review the definition of a class: a collection of abstracts of objects that share the same properties and functions. There are two key words: object and abstraction.

An object is a self-contained entity that can be identified by a set of identifiable characteristics and behaviors. For example, “human” is an object with recognizable characteristics such as eyes, nose and mouth, as well as recognizable behaviors such as eating, sleeping and writing code.

There are different properties between people such as male and female, different functions such as front-end developers and back-end developers, same properties such as eyes and noses, same functions such as eating and sleeping. The function of abstraction is to extract and process the same attributes and functions, and finally form a set called “human”. Then “human” is a class.

Then use an example to understand the single duty principle, such as if you are a landlord and rent a house. The tenants can be treated as objects, and the house as a class. The tenant (object) influences the rent that the house (class) brings to the landlord.

The single duty principle requires that the house be rented to only one tenant, and that only one tenant can affect the rent that the house brings to the landlord (a cause that causes a class change). If you rent to several people who do not know each other, there will be several people who will affect your rent income (several reasons for this type of change).

In code:

class House { constructor(param) { this.data = param; this.name = param.name; / /... } countMoney(data){ //... } payMoney(){ let data; / /... const money = this.countMoney(this.data) } }Copy the code

In the above code, the class House represents the House, which is a collection of tenants, countMoney is used to calculate the rent, and payMoney is used to pay the rent. When a new House(param) is created, a House is created and the tenant information is passed in through param.

Now imagine that the first tenant is only one person. Calculating rent is easy to calculate, and the logic in countMoney’s method is easy to write. As more tenants were added, the countMoney method had to be rewritten. The error also affected other tenants to pay the rent. Some tenants even stopped renting because they were not satisfied with the rent calculation rules, which affected the landlord’s rent income. So the house is only rented to one person, and the rent is best calculated, which will not affect the rent income of the landlord.

Of course, in real life, it is impossible to rent the house to only one person, but also to a family or a principal landlord. The landlord only pays the rent with one tenant, so that the countMoney method will not be modified frequently. Only after the tenant who pays the rent to the landlord changes, the countMoney method will be modified.

Having said that the tenant can’t be a person, there can’t be a single method in the responsibility, and even the responsibility may contain other sub-responsibilities. So how to separate many functions into corresponding responsibilities, and how to divide responsibilities, is the architecture design to consider things.

The single responsibility principle applies not only to classes, but also to ordinary functions, where a function handles only one event.

Functions and advantages

The function of the single responsibility principle is to control the granularity of the class, decouple the object and improve its cohesion. It has the following advantages:

  • Reduce class complexity. If a class is responsible for only one responsibility, the logic is certainly much simpler than if it were responsible for multiple responsibilities.

  • Improve class readability. As complexity decreases, readability increases.

  • Improve the maintainability of your code. It’s more readable, so it’s easier to maintain.

  • Risk reduction due to change. Change is inevitable, and if the single responsibility principle is followed well, when one function is modified, the impact on other functions can be significantly reduced.

The difficulties in

In the process of following the single responsibility principle, the biggest difficulty is how to divide the responsibilities. Too fine division will lead to too many classes and maintenance difficulties. Too coarse division may violate the single responsibility principle.