Please state the source of the article. Welcome to add Echo wechat (wechat id: T2421499075) for exchange and learning.


As we said earlier, the rule engine is essentially a rule engine that pulls out the if else completely. But is that all there is to our rules engine? Rules are everywhere, why do you have to use the rules engine to separate them? The rules engine must have solved some problems for us. This article focuses on the rules engine is the solution to what

Why use a rules engine?

  • If else the need to pull away is not the goal
  • It’s not that the rules engine has excellent encoding syntax

Look at it by example

Real demand analysis: new users, when new users, according to the location of the user to mark, such as: a user is Beijing, mark beipiao, is Guangzhou, mark Guangpiao, and so on. The first version of the requirements might look something like this

if ("Shanghai".equals(people.getAddress())) {
    people.setFlag("Float");
    peopleServer.insert(people);
}
if ("Beijing".equals(people.getAddress())) {
    people.setFlag("North drift");
    peopleServer.insert(people);
}
if ("Shenzhen".equals(people.getAddress())) {
    people.setFlag("Deep drift");
    peopleServer.insert(people);
}
if ("Guangzhou".equals(people.getAddress())) {
    people.setFlag("Wide drift"); peopleServer.insert(people); }...Copy the code
  • It feels like there’s a lot of if and else and we can extract some of the common things, and depending on the rules, we can apply the policy pattern, or the chain of responsibility pattern, to optimize this hierarchy.

The requirements version iteration above: The leader found that there were many users in a certain area, such as: In Beijing, if all the new employees in Beijing are marked by Beipiao, the number of users marked is nearly 100,000, which is not conducive to data analysis and revenue transformation. As a result, a condition is added to the conditions of Beijing: age range: 18-30 “Beipiao youth”, 30-40 “Beipiao elite households” and so on. Your code might look something like this

if ("Shanghai".equals(people.getAddress())) {
    people.setFlag("Float");
    peopleServer.insert(people);
}
if ("Beijing".equals(people.getAddress())) {
    if (people.getAge() > 18 && people.getAge() <= 30) {
        people.setFlag("Young Drifters in the North");
        peopleServer.insert(people);
    }
     if (people.getAge() > 30 && people.getAge() < 40) {
        people.setFlag("North Drift strength household"); peopleServer.insert(people); }... }if ("Shenzhen".equals(people.getAddress())) {
    people.setFlag("Deep drift");
    peopleServer.insert(people);
}
if ("Guangzhou".equals(people.getAddress())) {
    people.setFlag("Wide drift"); peopleServer.insert(people); }...Copy the code
  • Can we still use the policy pattern when requirements change only? Can you still use the chain of responsibility? Note to have a very important question, I this instance to highlight the if the else such rules, business code is omitted, but the development of the real, we generally in the if the else to write a lot of corresponding business code, according to our tag, pushed for a user, at the same time to write a push strategy and push messages. Then in this case our design model is no longer practical. The bottom line is that if design patterns are applied, the overall code architecture loses flexibility, there are too many commonalities, and too many things to change if the rules change again.

Why does version iteration disable many of our if else optimizations?

  • Separation of rules and business cannot be resolved in many places
  • Most optimization methods can not be directly or indirectly flexible allocation, many are the use of common extraction
  • Rule iteration, the optimization part is easy to pull the whole body

Can a rules engine solve this problem?

  • The greatest use of the rules engine is the flexibility to separate rules from business

conclusion

Rule engine solves the difficulty of changing rules flexibly without affecting specific business