【java规则引擎】规则引擎RuleBase中利用观察者模式

简介: (1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变。(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想。可以利用观察者模式实现。

(1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变。
(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想。可以利用观察者模式实现。基于易变动的数据结构中加入监听者,监听者依据被监听者的变动动作,定义自己的动作。在监听者内部通知其他组件做出相应改变。实现面向对象的思想。组建之间实现了解藕。

 

一:被监听数据,也就是规则网络RuleBase的接口定义

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 /**
 3  * 定义一个核心数据的对象的接口
 4  * 
 5  * @author sxf
 6  *
 7  */
 8 public interface RuleBase {
 9     /**
10      * 添加一个规则
11      * @param rule
12      */
13     public void addRule(String rule);
14     /**
15      * 删除一个规则
16      * @param rule
17      */
18     public void removeRule(String rule);
19     /**
20      * 修改一个规则
21      * @param rule
22      */
23     public void updateRule(String rule);
24 
25 }
View Code

二:被监听数据,也就是规则网络RuleBase的实现定义

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 /**
 3  * 真正的RuleBase对象
 4  * @author sxf
 5  *
 6  */
 7 public class ReootoRuleBase  implements RuleBase{
 8     
 9     private RuleBaseListernSupport support;
10 
11     @Override
12     public void addRule(String rule) {
13         support.addRule(rule);
14         System.out.println("ReootoRuleBase.addRule(添加规则的操作)");
15         
16     }
17 
18     @Override
19     public void removeRule(String rule) {
20         support.removeRule(rule);
21         System.out.println("ReootoRuleBase.removeRule(规则删除的操作)");
22     }
23 
24     @Override
25     public void updateRule(String rule) {
26     
27         
28     }
29 
30     
31 }
View Code

三:用于管理不同组件监听器的监听管理者。

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 
 3 import java.util.List;
 4 
 5 
 6 /**
 7  * 管理ruleBase某些动作的事件监听管理
 8  * 
 9  * (1)当ruleBase对象发生某个动作,都要告诉监听管理器,监听管理器做相应的处理
10  * (2)监听管理者,包括监听器的行为都是依据RuleBase的动作为定。
11  * @author sxf
12  *
13  */
14 public class RuleBaseListernSupport {
15     
16     /**
17      * 事件支持的监听器集合
18      */
19     private List<RuleBaseEventListern> listeners;
20 
21     /**
22      * 规则添加监听器发布事件
23      * @param rule
24      */
25     public void addRule(String rule) {
26         RuleBaseEvent event=new RuleBaseEvent(rule);
27         for(RuleBaseEventListern listener:listeners){
28             listener.ruleBaseAddRule(event);
29         }
30     }
31 
32     
33     /**
34      * 规则删除监听器发布事件
35      * @param rule
36      */
37     public void removeRule(String rule){
38         RuleBaseEvent event=new RuleBaseEvent(rule);
39         for(RuleBaseEventListern listener:listeners){
40             listener.ruleBaseRemove(event);
41         }
42     }
43 
44 
45     public List<RuleBaseEventListern> getListeners() {
46         return listeners;
47     }
48 
49 
50     public void setListeners(List<RuleBaseEventListern> listeners) {
51         this.listeners = listeners;
52     }
53     
54     
55     
56 }
View Code

四:各种组件监听器的接口定义

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 /**
 3  * (1)监听器接口,将来不同的组件监听RuleBase的变动,都可以声明一个监听器,实现该接口
 4  * (2)将监听器注册到RuleBase的监听管理者内部
 5  * @author sxf
 6  *
 7  */
 8 public interface RuleBaseEventListern {
 9     
10     /**
11      * 一个规则被添加
12      * @param event
13      */
14     public void ruleBaseAddRule(RuleBaseEvent event);
15     /**
16      * 一个规则被删除
17      * @param event
18      */
19     public void ruleBaseRemove(RuleBaseEvent event);
20 
21 }
View Code

五:各种监听器的监听事件的定义

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 /**
 3  * ruleBase的监听事件
 4  * @author sxf
 5  *
 6  */
 7 public class RuleBaseEvent {
 8 
 9     private Object source;
10 
11     public RuleBaseEvent(Object source) {
12         super();
13         this.source = source;
14     }
15 
16     public Object getSource() {
17         return source;
18     }
19 
20     public void setSource(Object source) {
21         this.source = source;
22     }
23     
24     
25 }
View Code

六:RuleBase变动日记记录的监听器

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 /**
 3  * ruleBase改变记录日志的监听器
 4  * @author sxf
 5  *
 6  */
 7 public class LogRecordRuleBaseListener implements RuleBaseEventListern {
 8 
 9     @Override
10     public void ruleBaseAddRule(RuleBaseEvent event) {
11         String rule=(String) event.getSource();
12         System.out.println("LogRecordRuleBaseListener.ruleBaseAddRule(一个规则被添加===>"+rule);
13         
14     }
15 
16     @Override
17     public void ruleBaseRemove(RuleBaseEvent event) {
18         String ruleString=(String) event.getSource();
19         System.out.println("LogRecordRuleBaseListener.enclosing_method(一个规则被删除)"+ruleString);
20         
21     }
22     
23     
24 
25 }
View Code

七:RuleBase变动通知用户的监听器

 1 package com.nonbankcard.commons.doorls.ruleBaseListern;
 2 
 3 public class NoticeSystemRuleBaseListener implements RuleBaseEventListern {
 4 
 5     @Override
 6     public void ruleBaseAddRule(RuleBaseEvent event) {
 7         String rule=(String) event.getSource();
 8         System.out.println("NoticeSystemRuleBaseListener.ruleBaseRemove(通知用户添加一个规则)"+rule);
 9         
10     }
11 
12     @Override
13     public void ruleBaseRemove(RuleBaseEvent event) {
14         String rule=(String) event.getSource();
15         System.out.println("NoticeSystemRuleBaseListener.ruleBaseRemove(通知用户删除了一个规则)"+rule);
16     }
17 
18 }
View Code

 

相关文章
|
19天前
|
设计模式 监控 Java
设计模式 - 观察者模式(Observer):Java中的战术与策略
【4月更文挑战第7天】观察者模式是构建可维护、可扩展系统的关键,它在Java中通过`Observable`和`Observer`实现对象间一对多的依赖关系,常用于事件处理、数据绑定和同步。该模式支持事件驱动架构、数据同步和实时系统,但需注意避免循环依赖、控制通知粒度,并关注性能和内存泄漏问题。通过明确角色、使用抽象和管理观察者注册,可最大化其效果。
|
4月前
|
设计模式 存储 Java
Java设计模式【二十】:观察者模式
Java设计模式【二十】:观察者模式
26 0
|
3月前
|
设计模式 前端开发 NoSQL
聊聊Java设计模式-观察者模式
观察者模式(Observer Design Pattern),也叫做发布订阅模式(Publish-Subscribe Design Pattern)、模型-视图(Model-View)模式、源-监听器(Source-Listener)模式、从属者(Dependents)模式。指在对象之间定义一个一对多的依赖,当一个对象状态改变的时候,所有依赖的对象都会自动收到通知。
66 0
聊聊Java设计模式-观察者模式
|
4月前
|
设计模式 Java
Java设计模式:什么是观察者模式(Observer Pattern)?
Java设计模式:什么是观察者模式(Observer Pattern)?
31 0
|
6月前
|
设计模式 Java API
【设计模式——学习笔记】23种设计模式——观察者模式Observer(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——观察者模式Observer(原理讲解+应用场景介绍+案例介绍+Java代码实现)
91 0
|
8月前
|
设计模式 Java
实时更新:解析Java设计模式中的观察者模式
在软件开发领域,设计模式是一组经过验证的最佳实践方法,用于解决各种常见问题。观察者模式是一种行为型设计模式,其目标是在对象之间建立一种依赖关系,使得一个对象的状态发生变化时,所有依赖于它的对象都会得到通知并自动更新。在本文中,我们将深入了解观察者模式的核心思想、应用场景以及它在Java中的实际运用。
64 0
|
8月前
|
设计模式 Java
java实现23种设计模式-观察者模式
java实现23种设计模式-观察者模式
31 0
|
9月前
|
设计模式 Java
Java设计模式解析:观察者模式的应用和实例
Java设计模式解析:观察者模式的应用和实例
|
9月前
|
设计模式 消息中间件 NoSQL
Java中23种面试常考的设计模式之观察者模式(Observer)---行为型模式
Java中23种面试常考的设计模式之观察者模式(Observer)---行为型模式
49 1
|
9月前
|
设计模式 Java
【设计模式】用Java实现观察者模式
观察者模式是一种行为设计模式,用于实现对象之间的发布-订阅机制。在该模式中,存在一个主题对象(被观察者),它维护了一个观察者列表,并在自身状态发生改变时通知所有观察者。观察者对象订阅主题对象的状态变化,并在收到通知后执行相应的操作。
53 0