Interviewer : I want to ask a question, a common question in the project
Interviewer : I now have a system that will take different actions according to the requested entry. However, the requirements of this different action are likely to change. How would you design this system?
Interviewer : Practical example: Now there are multiple third-party channels, and the system needs to attribute orders to various channels. However, the logic of attribution is likely to change, and the logic of attribution of different channels is not the same. At this time, the logic in the system is relatively complicated.
Interviewer : If you let you optimize, how would you design?
candidate : I understand what you mean
candidate : In the final analysis, the processing logic is relatively complicated, and there are too many if else judgments
candidate : Although new requirements are coming, you can add if else to solve them
candidate : But what you want is that the system is more scalable and maintainable
Candidate : Want me to come up with a solution to solve similar problems
Candidate : Right?
Interviewer : Well...
Candidate : Before this, most of them said it was a strategy mode on how to solve if else on the Internet.
Candidate : But the examples cited don’t empathize with each other. Many times it passes after reading.
candidate : Actually, in the project, there are still quite a lot of strategy patterns, which may have been used accidentally (after all, interface-oriented programming)
candidate : And I think that the strategy model is not the key to solving if else
Candidate : This question, the approach in my project is: the chain of responsibility model
candidate : extract each process separately into a Process (can be understood as a module or node), and then the request will be stuffed into the Context
Candidate : For example, if you have maintained a project before, it is similar to different channels using different logic
Candidate : What we do here is: extract relevant logic to Process, and assign different chains of responsibility for different channels
candidate : For example, the responsibility chain of channel A is: WhiteListProcess->DataAssembleProcess->ChannelAProcess->SendProcess
candidate : The responsibility chain of channel B is: WhiteListProcess->DataAssembleProcess->ChannelBProcess->SendProcess
candidate : On the basis of the chain of responsibility, "scripts" can be embedded in the code
candidate : For example, on SendProcess, a script for sending messages is built-in (the script can select different operators to send messages). With the "script", you can make changes to the logic take effect without restarting.
candidate : Some people call this set of things a "rule engine". For example, the well-known implementation framework "Drools" in the rule engine can do similar things.
candidate : write the logic that is easy to change on the "script" (at least we think that the script and the real logic of our application are separated)
candidate : (The script here refers to the rule set, which can be Drools dsl, Groovy, aviator, etc.)
Interviewer : Well...
Candidate : At my previous company, I used Groovy scripts. The general implementation logic is: there is a special backend to manage the script, and then the script will be written to the "distributed configuration center" (real-time refresh), and the client monitors whether the script stored in the "distributed configuration center" has changed
candidate : If there are changes, recompile and load the script through the Groovy class loader, and finally put it in the Spring container for external use
Candidate : This is how the system I am currently responsible for deals with business that is changing and frequently changing requirements (responsibility chain + rule engine)
Candidate : But as far as I know, our gameplay business has done more in the "responsibility chain"
candidate : The "responsibility chain" is no longer written from the code, but sinks to the platform to do "service orchestration", that is, programmers go to the "service orchestration backend" to configure information (configure each node of the responsibility chain) )
Candidate : Clients who use "Service Orchestration" in the business system, as long as the ID of "Service Orchestration" is passed in when requesting, the code can be executed according to the process of "Service Orchestration"
candidate : The advantage of this is that the business chain is configured in the background, and there is no need to maintain the chain on the system business, which is more flexible (the written responsibility chain nodes can be combined at will)
Interviewer : Then I understand
Welcome to follow my WeChat public [161a031a0c08b4 Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!
[Online Interviewer-Mobile] The series updated twice a week!
[Online Interviewer-Computer] The series updated twice a week!
Originality is not easy! ! Seek three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。