我有一个要求,它需要针对 java 值对象验证大量规则并产生结果。(我们公司不能使用任何规则引擎应用程序,手续繁多,需要回答的问题很多)。所以,与其在java代码中实现规则,我建议实现一个小的规则引擎,简单和可扩展。遵循哪种设计模式?
我在下面添加了一个粗略的定义规则的 xml 结构。
<rule-set>
<name>Example1</name>
<description>Example rules defined</description>
<beans>
<bean class="com.example.Customer" alias="cust"/>
<bean class="com.example.Account" alias="acnt"/>
<bean class="com.example.Transaction" alias="trans"/>
</beans>
<rule name="CustomerInfo" description="This rule validates if all the customer values are present">
<if lhs="cust.getFirstName" rhs="null" operator="!="/>
<if lhs="cust.getLastName" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getCountry" rhs="null" operator="!=" logicaloperator="||"/>
<if lhs="cust.getCity" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getPhone" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getEmail" rhs="null" operator="!=" logicaloperator="&&"/>
<then do="cust.completeFlag" arg1="true"/>
</rule>
<rule name="Transaction" description="Transfer the money from one ac to another">
<if lhs="trans.fromAccount" operator="!=" rhs="null"/>
<if lhs="trans.toAccount" operator="!=" rhs="null"/>
<if lhs="trans.fromAccount.balance" operator=">" rhs="trans.getTransaferAmount"/>
<then do="trans.fromAccount.debit" arg1="trans.getTransaferAmount"/>
<then do="trans.toAccount.credit" arg1="trans.getTransaferAmount"/>
</rule>
</rule-set>