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 design principles of SOLID

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the design principles of SOLID". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The SOLID principle is the five basic principles of object-oriented programming and object-oriented design introduced by Robert C. Martin in the early 21st century. They are SRP (single responsibility principle), OCP (open and closed principle), LSP (Richter substitution principle), ISP (interface separation principle) and DIP (dependency inversion principle). It is typically used in test-driven developers, and it is also an important part of the basic principles of agile development and adaptive software development.

Principle of single responsibility (SRP)

The principle of single responsibility holds that a class should have only one function. In program development, there is only one reason why a class needs to be modified. Assume that this class needs to take on other types of responsibility, then decompose the class. There is a great chance that the class will be modified. If you put multiple functions in the same class, there is a relationship between the functions. If you change one function, it is possible to stop the other. At this time, a new round of testing is needed to avoid possible problems and greatly damage its cohesion and coupling.

Open and closed principle (OCP)

Normally, software entities should be extensible, but not modifiable. The extension here means openness, while not being modified means closeness. In general, this principle is the most abstract and difficult for programmers to understand among the many object-oriented programming principles in the development process.

Richter substitution principle (LSP)

The object in the program should be replaceable by its subclasses without changing the correctness of the program. The principle of Richter scale substitution is an important guarantee to make the code conform to the principle of open and closed. An is-A relationship exists only when an instance of a subclass should be able to replace any instance of its superclass. The customer module should not care about how the service module works; the same interface modules can be replaced without knowing the service module code. That is, where the interface or parent class appears, the class or subclass that implements the interface can be substituted.

Interface separation principle (ISP)

Users cannot be forced to rely on interfaces they do not use. In other words, it is better to use multiple specialized interfaces than to use a single master interface. Note that applying ISP to your code does not necessarily assume that this service is absolutely secure, but also requires a good coding time to ensure correct authentication and authorization. For example, in Java, a class implements multiple interfaces, and different interfaces are used by customer modules that are not in use, rather than providing a large interface to customer modules.

Dependency injection or inversion principle (DIP)

The principle of dependency injection or inversion stipulates that the code should depend on the abstract concept, not the concrete implementation, which can be divided into two parts: 1. High-level modules should not rely on low-level modules, both should rely on abstraction; 2. Abstraction should not depend on details, which should depend on abstractions.

The highlight of this design principle is that any class injected by the DI framework is easy to test and maintain with mock objects, because the object creation code is concentrated in the framework and the client code is not cluttered. There are many ways to implement dependency inversion, such as bytecode techniques used by AOP (Aspect Oriented programming) frameworks such as AspectJ, or proxies used by Spring frameworks.

This is the end of the content of "what are the design principles of SOLID". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report