观察者模式
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。
观察者模式使用三个类 Subject、Observer 和 Client。
代码实例
Observer类
public abstract class Observer {
private String phone;
public abstract void handleException(String message);
public Observer(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Observer实体类
public class ServerObserver extends Observer {
public ServerObserver(String phone) {
super(phone);
}
@Override
public void handleException(String message) {
System.out.println("服务器运维人员根据异常信息检查服务器");
}
}
public class BIZObserver extends Observer {
public BIZObserver(String phone) {
super(phone);
}
@Override
public void handleException(String message) {
System.out.println("业务运维人员根据异常信息检查业务功能");
}
}
public class DBObserver extends Observer {
public DBObserver(String phone) {
super(phone);
}
@Override
public void handleException(String message) {
System.out.println("db运维人员根据异常信息检查数据库");
}
}
Subject类
public abstract class Subject {
protected List<Observer> observers = new ArrayList<>();
public abstract void addObserver(Observer observer);
public abstract void attach(Observer observer);
public abstract void notifyAllObserver(String msg);
}
Subject实体类
自动通知(放在全局异常处理器中)
public class AutoSubject extends Subject{
public AutoSubject(){
addObserver(new DBObserver("138867654654"));
addObserver(new BIZObserver("13876598766"));
addObserver(new ServerObserver("13876598788"));
}
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void attach(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyAllObserver(String msg) {
for (Observer observer : observers) {
SendSMS.sendMsg(observer.getPhone(),msg);
System.out.println("给"+observer.getPhone()+"发送了一条短信");
observer.handleException(msg);
}
}
}
public class AdminSubject extends Subject{
public AdminSubject(){
addObserver(new DBObserver("138867654654"));
addObserver(new BIZObserver("13876598766"));
}
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void attach(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyAllObserver(String msg) {
for (Observer observer : observers) {
SendSMS.sendMsg(observer.getPhone(),msg);
System.out.println("给"+observer.getPhone()+"发送了一条短信");
observer.handleException(msg);
}
}
}
Client
全局异常处理自动通知运维人员
public class GlobalExceptionHandler extends HandleExceptionResolver {
@Override
public void handleException(Exception e, HttpServletRequest request, HttpServletResponse resp) {
System.out.println("进入了全局异常处理器");
//返回异常
e.printStackTrace();//自己调试看的
//处理异常---》DataResult
DataResult dr = null;
String msg = e.getMessage();
if(e instanceof InvocationTargetException){
InvocationTargetException ex = (InvocationTargetException) e;
Throwable exp = ex.getTargetException();
if(exp instanceof BussinessException){
dr = new DataResult(((BussinessException) exp).getId(),exp.getMessage());
}else if(exp instanceof SQLException){
dr = new DataResult(ResponseCode.SQL_EXCEPTION);
}else{
dr = new DataResult(ResponseCode.OTHER_EXCEPTION);
}
msg = exp.getMessage();
}else{
dr = new DataResult(ResponseCode.OTHER_EXCEPTION);
}
/**************此处自动通知***************/
new AutoSubject().notifyAllObserver(msg);
resp.setContentType("text/html;charset=utf-8");
try {
PrintWriter out = resp.getWriter();
out.write(JSON.toJSONString(dr));
out.flush();
out.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}