Why write about this topic?

Solutions with a layer in the middle of software design are everywhere. The purpose of writing this article is to see many scenarios based on the application of this idea, SO I want to sort out and let you see some essential content.

By the way, some of today’s mainstream technologies or concepts as samples, to help you understand.

Understand the underlying logic and design ideas, will not be a variety of technical terms, technical concepts of the whole face of the muddled force.

Now omnipresent three high explanation, training, be careful to learn, don’t whole body three high, ha ha, just kidding.

But software design thought level of things, some programming thinking, we also need to learn to improve, thinking precipitation. There are more than three high points in learning technology.

Moreover, thinking is not like learning a specific technical framework, which can be used after a few days or even hours of wholehearted devotion. It’s about accumulating it bit by bit, thinking about it bit by bit.

It is not good to see others’ summary, others’ is always others’, reference can, must be summed up by themselves.

Back to the theme, today’s post is “On the Power of adding a layer in software Design”.

The text start

The JVM you’re familiar with

Let’s start with a simple JVM execution diagram.


By adding a JVM between the class and the operating system, when a new operating system comes along, you just need to implement a set of JVMS for that platform (the JVM’s platform implementations are not our concern, the official team handles them) and the project can run on that platform, which is what Java claims to be writing once, running everywhere. To run anywhere, you need to have an implementation of the JVM everywhere.

The JVM here acts as an intermediate layer, separating the development environment from the operating system.

The benefit of isolation is that it eliminates the interaction and maladjustment of direct contact between two parties, which becomes invisible at the JVM level.

For project developers, I don’t care what the underlying operating system is, I just connect to the JVM. Any changes or upgrades to the operating system are none of my business, they are all for the JVM to consider.

For the operating system, there is no need to make any changes to run your class. This is also something that the JVM needs to consider.



Design patterns that don’t know where to use them

The proxy pattern is one of the simpler and easier to understand design patterns. Take a look at the diagram.









The appearance model













Message middleware

Here mainly said in message middleware decoupled scenario, eliminate the dependence between the two services, with the help of message middleware can be realized, no message middleware can realize, can completely, their realization of an intermediate exchange system, why not just need to put in time and effort, so everyone use ready-made.

In this scenario, just decoupling the dependencies between the two services is nothing to brag about. Its real power lies in the addition of an intermediate layer that transforms the many-to-many invocation structure between multiple services into a many-to-one structure. The following figure

The figure on the right is not a little clearer than the figure on the left. If the figure can be seen clearly, the code implementation has the premise of writing clearly.



The gateway

So let’s look at a picture

With gateway as the middle layer, on the one hand, the access address of the real service can be hidden; on the other hand, the call entrance can be unified for the caller; at the same time, global processing logic can be unified for all back-end services in the gateway layer, such as authentication, traffic limiting, logging and so on.



One last scenario from the project

A scenario encountered in the previous project was that it was necessary to output a result that the customer wanted through a very complex statistical SQL. When the data volume was large, the query was very slow. How to deal with this problem when the real-time requirement of data was not high and redundant technical components were not introduced?

The solution is to add an intermediate result table, the periodic execution of statistical SQL results into the results table, query function directly query results table, it is very convenient to solve. The processing process is shown below

The above is only a partial list, what is not mentioned is left to the reader to find out.



conclusion

Combined with the above examples, it is concluded that the design of adding a layer in the middle can probably solve the problems of the following scenarios.

It is particularly emphasized here that the next layer is a design idea, which can not only solve technical problems, but also be used in daily life. Therefore, the key is to absorb this design idea.

Let’s go back to the technical scenario and say that different scenarios are handled differently, and you don’t have to have a technical component so that, for example, in certain scenarios, you can add a field, you can add a class. Be sure to learn and apply.

decoupled

The purpose of decoupling is to be extensible, maintainable, improve the modifiability of software and ensure their independent evolution.

The aggregation

To simplify upper-level calls and get results, add an aggregation middle layer, which has a bit of uncoupling and hiding of details, which is not the focus of aggregation, but it does.

Unified handling

Typical scenarios The application scenarios and functions of the gateway, such as the authentication center and logging, can be implemented at the gateway layer in a unified manner.

Hide details

Does the system have any little secrets of its own that it doesn’t want the outside world to know? Add an external call layer, hide the real service address, change the method name, change the parameters you can do anything you want.

Shielding differences

The corresponding scenario not mentioned above is more appropriate for adapters in design mode. A typical scenario is how a two-plug plug is connected to a three-way socket. You can do that by adding a switch header. The differences between interfaces are shielded indirectly through adapters.

The same applies to systems where one interface outputs XML and the other receiver needs JSON. What to do is to add an intermediate conversion layer when neither side can change. Mask differences in data packets.



The last

In fact, a good design can avoid some technical problems to a certain extent and simplify the problem scene, which requires us to constantly explore, try, learn and summarize.

You think it’s okay. Give it a thumbs up.

That’s all for today, and I’ll see you next time.

More quality content, the first public number [wind elephant south], welcome to pay attention to.