Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advantages of java's single responsibility principle?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what are the advantages of the single responsibility principle of java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the advantages of java's single responsibility principle"?

Single responsibility principle (SRP), The Single Responsibility Principle

Define

There can only be one reason why a class can be modified.

Generally speaking, a class can only be responsible for one responsibility, and modifying a class does not affect other functions, that is to say, there is only one reason for the class to be modified. All of us who write code know that we should try our best to achieve the characteristics of low coupling and high cohesion, and the single responsibility principle ensures the low coupling between classes. If a class takes on too much responsibility, there will be many reasons for the class to be modified, and there is a good chance that other functions will be affected.

The principle of single responsibility seems to be a very simple principle, but it is not easy in practice, because the combination of responsibilities is often encountered in practice, and we cannot casually disassemble and classify them to adapt to a single duty model. therefore, how to reasonably separate duties from these joint duties and more appropriately abide by the principle of single responsibility should be carefully considered.

See if the following interface conforms to the principle of single responsibility?

Public interface UserInterface {

Void saveUser (User user)

User getUser (long id)

Void updateUserBalance (long id, BigDecimal balance)

BigDecimal getUserBalance (long id)

}

This is a user interface, which provides four methods: save the user, get the user, update the user balance, and obtain the user balance. Obviously, the user's personal information and the user's account balance are two different things, so the design is highly coupled. It is not conducive to expansion and does not conform to the principle of single responsibility. We can break it into two, one for user information interface and one for account interface, as follows.

Public interface UserInterface {

Void saveUser (User user)

User getUser (long id)

}

Public interface AccountInterface {

Void updateUserBalance (long id, BigDecimal balance)

BigDecimal getUserBalance (long id)

}

In this way, is it in line with the principle of single responsibility, and the complexity and coupling of the class are reduced, even if the user interface or account interface addition or subtraction interface does not affect other interface implementation classes.

Therefore, the principle of single responsibility can be summarized as the following advantages:

1. Low coupling and small range of influence.

2. The complexity of the class is reduced, the responsibility is clear, and the readability is improved.

3. The responsibility is single, which is beneficial to maintenance.

At this point, I believe you have a deeper understanding of "what are the advantages of the single responsibility principle of java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report