many java developers today have moved toward some form of logging and/or unit test framework, and their code has been purged of many system.out. println() statements that were the traditional approach. now perhaps its time to get rid of some of those if ( x ) {. . .} as well. nothing in a piece of code seems to foul up the design as much as the business logic, and going from two-tier to three-tier to n-tier hasnt done much to solve that problem.
in some dark layer of the code there is still a tangled mass of ifs, elses, and look-up tables upon which the whole thing rests. after a project goes through a few generations of developers, the "business rule" layer becomes a gordian knot that defies both change and comprehension. rule engines offer a framework for isolating the business logic in your applications; this framework is simpler and more flexible than look-up tables. as a design approach, it fits java programs of almost every conceivable form, purpose, and budget. most important, it brings the elusive goal of code reuse a step closer. this article lays the foundation for getting started using a rule engine on your own project.
what is a rule engine?
a rule engine is a system for applying some set of if/then rules to the data in a system and then taking some action on the data or the system itself. its primary purpose is to separate the business logic from the system logic - to externalize the business logic so it can be maintained separately. use a rule engine to separate the if/then rules from system code - to externalize them so they can be maintained separately from the system code. the behavior of the system can then be modified without changing code or needing to recompile/redeploy. rules are stored in human-readable form in a file so they can be changed with a text editor or rule editor.for example, a typical storefront system might involve code to calculate a discount:
if (product.quantity > 100) {
product.discount = 2;
} else if (product.quantity >= 500 && product.quantity < ... 下一页