One article explains the idea of “middle layer”

Author: Senior Mingming Ruyue, CSDN blog expert, senior Java engineer of Dachang, author of "Performance Optimization Methodology", "Unlocking Dachang Thinking: Analysis" Alibaba Java Development Manual "", "Learning Classics Again: Exclusive Analysis of "EffectiveJava"" columnist.

Hot Gate Text Recommendation

A thorough understanding of the idea of ​​“middle layer” in one article.png

I. Introduction

I believe many people have heard of a very famous statement by David Wheeler: "Any problem in the field of computer science can be solved by adding an indirect middle layer.
image.png
As the saying goes: “Experts watch the door, while laymen watch the excitement.” In my opinion, many people think they understand, but they still stay at the level of "watching the excitement" and cannot truly understand the meaning of the "middle level" thinking, and it is even more difficult to "unify knowledge and action" for general application.

So, what exactly is the meaning of middle-level thinking? What are some unexpected application scenarios of middle-layer thinking? What inspirations can middle-layer thinking bring to us? Can the middle layer really solve everything? This article will reveal it for you!

2. What is the idea of ​​“middle layer”?

2.1 Explanation

image.png

"All problems in computer science can be solved by adding an extra layer of indirection" is a humorous statement on the importance of the indirection layer in computer science and software engineering. Layers of indirection can help solve a variety of problems, such as organizing, optimizing, and reusing code.

Here are several aspects of the meaning of this sentence:

  1. Decoupling:
    By adding layers of indirection, different parts of the system can be separated to minimize dependencies between them. This way, if one part changes, it doesn't directly affect other parts.
  2. Abstraction:
    The indirection layer can provide an abstraction that allows developers to work without having to worry about the underlying implementation details. For example, a database management system (DBMS) provides developers with an abstraction for querying and managing data without having to worry about the underlying data storage and retrieval mechanisms.
  3. Scalability:
    Layers of indirection can help design scalable systems. For example, by adding a load balancing layer, requests from clients can be distributed to multiple servers, thereby increasing the system's processing capabilities.
  4. Reuse and Modularity:
    Layers of indirection allow you to create reusable and modular code. This allows developers to reuse the same code in different projects or systems, thereby improving development efficiency and code quality.
  5. Error handling and security:
    The indirection layer can also be used for error handling and security control. For example, by adding an error detection and retransmission layer to network communications, you can ensure the correct transmission of data.
  6. Optimization:
    A layer of indirection can be used to optimize performance, for example, by adding a caching layer to reduce access to underlying resources, thus improving System response speed.

2.2 Caseimage.png

The operating system connects software and hardware by adding an intermediate layer; the Java virtual machine realizes Java's cross-platform by adding a virtual machine layer; the distributed cache realizes caching between different applications by adding an intermediate cache layer Sharing; distributed locks realize cross-machine concurrency control by adding a locking mechanism in the middle layer; sub-database and sub-table middleware reduces complexity by adding a middle layer to encapsulate sub-database and sub-table logic; message queues are implemented through the middle layer Peak and asynchronous functions; the proxy service implements the function of accessing the target server through the middle layer proxy; the intermediary mode reduces direct communication between objects, reduces object coupling, and improves system flexibility by adding an intermediary middle layer. stability and maintainability; domain-driven, by adding an intermediate domain layer to achieve the stability of core business, improve cohesion and reduce coupling.
In the field of software engineering, there are many examples of solving problems through middle layers.

There are many examples of the "middle layer" in life. For example, payment is made through Alipay, shopping is made through Taobao, taxi-hailing is made through Didi, takeout is purchased through Ele.me, communication between different languages ​​is made through translation software or translators, people are introduced through acquaintances, etc.

Recently, GPT-4 on the ChatGPT official website also supports calling DALL·E3 to draw through natural language, which also reflects the idea of ​​​​"middle layer".image.png

Chinese users can generate satisfactory graphics through simple Chinese natural language descriptions.
image.png
ChatGPT, as the "middle layer", can convert the user's relatively simple Chinese natural language prompt words into the English professional and complex prompt words required by the DALL·E3 model, reducing the user's The threshold of use is very user-friendly.

3. Why is the idea of ​​"middle layer" so effective?

In my opinion, "middle layer" and "task decomposition" have the same purpose.

The "middle layer" is a specific application that uses "task decomposition" to decompose complex problems into simpler and more specific subtasks, reduce the complexity of the problem, and improve the efficiency and quality of problem solving.
image.png

The "middle layer" can reduce complexity mainly because it plays the role of "isolation" and "bridge", allowing clear boundaries and interaction methods between different layers or components.

4. What should we do?

4.1 Apply what you learn

The knowledge we learn is valuable only if we can "apply what we have learned" and use the knowledge we have learned to solve practical problems.
image.png

4.1.1 Assist in understanding knowledge

Since the idea of ​​"middle layer" has been widely used in the computer field, when we learn or review some computer professional basics, middleware, and design patterns, we need to actively think about whether the knowledge we are currently learning is through the "middle layer" Those who come to solve problems may be able to think of the essence of "sudden enlightenment" at a certain moment.

For example, whenwe learn domain drivers, we will find that it is quite consistent with the idea of ​​​​"middle layer". You can think about what problems it solves and what benefits it brings by using the idea of ​​​​the middle layer.

Problems solved by domain drivers:

  1. Decoupling: The domain layer separates business logic from other layers (such as UI layer, data access layer), reducing the coupling between layers.
  2. Maintainability: Business logic is concentrated in the domain layer, making the code easier to maintain and test.
  3. Reusability: Business logic in the domain layer can be reused in different applications or systems.
  4. Easy to understand: By using domain-specific language (Ubiquitous Language), the business logic is easier to understand.

Benefits brought by domain driving:

  1. Agile development: Since business logic is decoupled from other layers, it is easier to adapt to changes in requirements.
  2. Team collaboration: Using domain-specific languages ​​helps improve communication efficiency between developers and business people.
  3. High-quality code: The domain layer emphasizes software quality, such as code testability and scalability.
  4. Improve productivity: Through the application of patterns and design principles (such as factory pattern, strategy pattern, etc.), the domain layer can be developed faster.

For another example, when we learn the "decorator" design pattern, we can also think according to this model. The same goes for learning the adapter pattern, agent pattern, mediator pattern, etc.

Problems solved by the decorator pattern:

  1. Opening and closing principle: The decorator pattern allows you to add new functions to the original class without modifying the code.
  2. Single responsibility principle: Each decorator only cares about a specific functional extension, making the design of the class more modular.
  3. Flexibility: Features can be added or removed dynamically, at runtime.

Benefits of the decorator pattern:

  1. Maintainability: Since new functionality is implemented by adding new decorator classes, it is easier to unit test and maintain.
  2. Reusability: Decorators are independent objects that can be reused on different scenes and objects.
  3. Extensibility: New functionality can be easily implemented by adding new decorators without modifying existing code.
  4. Granular control: The decorator pattern provides a finer-grained way to extend functionality. You can choose to decorate only a specific object instead of the entire class.

The decorator design pattern provides a flexible and extensible way to increase the functionality of objects by introducing a decorator "middle layer" while keeping the original object unchanged. Not only does this help keep your code clean and maintainable, it also improves its reusability and scalability.

For another example, when we are learning API gateway, we can also think according to this model.
API gateway embodies the idea of ​​"middle layer" because it is located between the microservice architecture (or other server-side architecture) and the client, serving as an intermediary layer to manage and route requests. API gateways usually provide a range of functions, including request routing, load balancing, authentication, authorization, caching, etc.

Problems solved by API Gateway:

  1. Decoupling: The API gateway makes the interaction between the client and the server more decoupled. The client does not need to know the specific location and implementation details of the back-end service.
  2. Centralized management: All incoming and outgoing requests will pass through the API gateway, which facilitates monitoring, logging, security control, etc.
  3. Protocol conversion: The API gateway can be responsible for converting external HTTP/REST requests into other protocols used internally (such as gRPC, MQTT, etc.).
  4. Flexibility: Routing rules can be dynamically changed, new services added, or versioned as needed.

Benefits of API Gateway:

  1. Simplifying the client: Since the API gateway provides a unified entrance, the client implementation will be simpler.
  2. Improved security: Authentication and authorization are centralized, making it easier to securely control the system.
  3. Improve maintainability: Centralized management makes code and configuration easier to maintain.
  4. Performance optimization: API gateways usually include functions such as caching, current limiting, and fusing, which help improve the overall performance of the system.

Through this thinking mode, it is easier to grasp the essence of the problem and better understand the corresponding concepts.

4.1.2 Assist in problem solving

image.png
When we clarify our needs, need to design technical solutions, and encounter difficult-to-understand problems, we can proactively think about whether we can solve the current problems by adding a "middle layer". Sometimes there will be "miraculous results."

4.2 No silver bullet

Computer scientist Fred Brooks proposed the concept of "no silver bullet" in his classic paper "No Silver Bullet: Essential and Subsidiary Work in Software Engineering". That is, no single technology or method can solve all problems in software engineering, nor can it significantly improve the productivity of software engineering in a short period of time. Software engineering is a complex and difficult job that requires the comprehensive use of a variety of technologies and methods to solve various problems.
image.png
"The Art of Problem Solving" also mentioned that "solving one problem may lead to the creation of other problems or the worsening of the original problem."
Therefore, the "middle layer" thinking is not a "silver bullet" and is not omnipotent. When we use the "middle layer" thinking to solve problems, we need to think about "what side effects it brings." Weigh the pros and cons.

5. Summary

This article introduces the concept of "middle layer" and gives many examples that reflect the idea of ​​"middle layer" from the fields of software engineering and life to help everyone understand this concept. I hope that everyone can truly apply what they have learned, and use "middle layer" thinking to help us understand knowledge, and use "middle layer" thinking to help us solve problems. In addition, we also need to remember that "there is no silver bullet" and that everything has "pros and cons". When using the "middle layer" thinking to solve problems, we also need to weigh the pros and cons and make the best choice.


I am participating inCSDN 1024 Programmer Event. If this article is helpful to you, please like, bookmark and follow it for your support. And encouragement is the biggest motivation for my creation.
Insert image description here

Guess you like

Origin blog.csdn.net/w605283073/article/details/134067961