Java设计模式应用——观察者模式
告警结果产生后,可能需要发送短信,邮件,故障管理系统。这些转发操作不应当影响告警生成入库,并且类似事件可能根据不同场景,客户习惯不同,此时,使用观察者模式则可以很好的适应上述场景。
观察者模式应当包括:
1、 观察事件;
2、 观察者接口;
3、 若干观察者;
4、 观察者注册管理中心;
下面,我们使用观察者模式来开发告警短信、邮件转发功能;
1、 告警生成事件;
package com.coshaho.learn.observer; /** * * AlarmEvent.java Create on 2017年5月31日 下午10:59:24 * * 类功能说明: 告警结果事件 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public class AlarmEvent { private String alarmId; private String eventType; public AlarmEvent(String alarmId, String eventType) { this.alarmId = alarmId; this.eventType = eventType; } public String getAlarmId() { return alarmId; } public void setAlarmId(String alarmId) { this.alarmId = alarmId; } public String getEventType() { return eventType; } public void setEventType(String eventType) { this.eventType = eventType; } }
2、 告警生成观察者接口;
package com.coshaho.learn.observer; /** * * AlarmObserver.java Create on 2017年5月31日 下午11:22:43 * * 类功能说明: 观察者接口 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public interface AlarmObserver { public void run(AlarmEvent event); }
3、 邮件转发观察者;
package com.coshaho.learn.observer; public class EmailSendObserver implements AlarmObserver{ public void run(AlarmEvent event) { System.out.println("Send email success. Alarm id is " + event.getAlarmId() + ", operation type is " + event.getEventType()); } }
4、 短信转发观察者;
package com.coshaho.learn.observer; public class SMSSendObserver implements AlarmObserver{ public void run(AlarmEvent event) { System.out.println("Send SMS success. Alarm id is " + event.getAlarmId() + ", operation type is " + event.getEventType()); } }
5、 观察者注册管理中心;
package com.coshaho.learn.observer; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * * ObserverManager.java Create on 2017年5月31日 下午11:15:44 * * 类功能说明: 观察者注册管理中心 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public class ObserverManager { List<AlarmObserver> list = new ArrayList<AlarmObserver>(); public void add(AlarmObserver observer) { list.add(observer); } public void remove(AlarmObserver observer) { list.remove(observer); } public void notify(AlarmEvent event) { for(AlarmObserver observer : list) { poolService.execute(new ObserverThread(observer, event)); } } private ExecutorService poolService = Executors.newFixedThreadPool(5); public class ObserverThread implements Runnable { private AlarmObserver observer; private AlarmEvent event; public ObserverThread(AlarmObserver observer, AlarmEvent event) { this.observer = observer; this.event = event; } public void run() { observer.run(event);; } } }
6、 测试类;
package com.coshaho.learn.observer; /** * * ObserverTest.java Create on 2017年5月31日 下午11:26:29 * * 类功能说明: 测试 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public class ObserverTest { public static void main(String[] args) { AlarmEvent createEvent = new AlarmEvent("1", "create"); AlarmEvent updateEvent = new AlarmEvent("1", "update"); ObserverManager manager = new ObserverManager(); AlarmObserver emailObserver = new EmailSendObserver(); AlarmObserver smsObserver = new SMSSendObserver(); manager.add(emailObserver); manager.add(smsObserver); manager.notify(createEvent); manager.notify(updateEvent); } }
7、 结果。
Send email success. Alarm id is 1, operation type is create Send SMS success. Alarm id is 1, operation type is create Send email success. Alarm id is 1, operation type is update Send SMS success. Alarm id is 1, operation type is update