观察者模式
# 为什么使用观察者模式?
# 模型架构
# 模拟抽奖
public class Lottery {
public String lottery(String uId) {
Date date = new Date();
return Math.abs(date.hashCode()) % 2 == 0 ? "恭喜你,编码".concat(uId).concat("在本次摇号中签") : "很遗憾,编码".concat(uId).concat("在本次摇号未中签或摇号资格已过期");
}
}
1
2
3
4
5
6
2
3
4
5
6
@Data
@AllArgsConstructor
public class Result {
private String uId;//用户
private String msg;//摇号信息
private Date dateTime;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 监听事件
public interface EventListener {
void doEvent(Result result);
}
1
2
3
2
3
public class MessageEventListener implements EventListener{
private Logger logger = LoggerFactory.getLogger(MessageEventListener.class);
@Override
public void doEvent(Result result) {
logger.info("给用户 {} 发送短信通知(短信):{}", result.getUId(), result.getMsg());
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
public class MQEventListener implements EventListener{
private Logger logger = LoggerFactory.getLogger(MQEventListener.class);
@Override
public void doEvent(Result result) {
logger.info("记录用户 {} 摇号结果(MQ):{}", result.getUId(), result.getMsg());
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 事件管理容器
public class EventManager {
public enum EventType {
MQ,Message
}
Map<Enum<EventType>, List<EventListener>> listeners = new HashMap<>();
public EventManager(Enum<EventType>... operations) {
for (Enum<EventType> operation : operations) {
this.listeners.put(operation, new ArrayList<>());
}
}
public void subscribe(Enum<EventType> eventType, EventListener listener) {
List<EventListener> eventListeners = listeners.get(eventType);
eventListeners.add(listener);
}
public void unsubscribe(Enum<EventType> eventType, EventListener listener){
List<EventListener> eventListeners = listeners.get(eventType);
eventListeners.remove(listener);
}
public void notify(Enum<EventType> eventType, Result result){
List<EventListener> eventListeners = listeners.get(eventType);
for (EventListener eventListener : eventListeners) {
eventListener.doEvent(result);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 业务抽象类
public abstract class Service {
private EventManager eventManager;
public Service() {
eventManager = new EventManager(EventManager.EventType.MQ,EventManager.EventType.Message);
eventManager.subscribe(EventManager.EventType.MQ,new MQEventListener());
eventManager.subscribe(EventManager.EventType.Message,new MessageEventListener());
}
public Result draw(String uid){
Result result = doDraw(uid);
eventManager.notify(EventManager.EventType.MQ,result);
eventManager.notify(EventManager.EventType.Message,result);
return result;
}
protected abstract Result doDraw(String uid);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 业务实现类
public class ServiceImpl extends Service{
private Lottery l = new Lottery();
@Override
protected Result doDraw(String uid) {
String res = l.lottery(uid);
return new Result(uid,res,new Date());
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 测试验证
public class test {
private Logger logger = LoggerFactory.getLogger(test.class);
public static void main(String[] args) {
Service service = new ServiceImpl();
Result result = service.draw("0001");
log.println(JSON.toJSONString(result));
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9