用JDK的中观察者模式实现对温度的监控。每两秒钟会去实时的监测环境的温度,环境温度低于35度时为正常温度,否则为异常温度,当连续监测超温达到临界值时,会向配置的邮箱发送警示邮件
1定义温度监测条件
/**
* @author lyf
* @projectName designPattern
* @date 2022/5/24 下午 06:13
* @description 温度监控条件
*/
public class Condition {
//临界温度
private int limitTemperature;
//连续达到临界次数
private int limitCount;
//连续达到临界次数发送的邮件对象
private String mail;
public Condition() {
}
public Condition(int limitTemperature, int limitCount, String mail) {
this.limitTemperature = limitTemperature;
this.limitCount = limitCount;
this.mail = mail;
}
public int getLimitTemperature() {
return limitTemperature;
}
public void setLimitTemperature(int limitTemperature) {
this.limitTemperature = limitTemperature;
}
public int getLimitCount() {
return limitCount;
}
public void setLimitCount(int limitCount) {
this.limitCount = limitCount;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
2定义温度变化的主题
/**
* @author lyf
* @projectName designPattern
* @date 2022/5/24 下午 06:16
* @description 溫度數值監控器
*/
public class TemperatureSubject extends Observable {
private int randomTemperature;
private Condition condition;
public Condition getCondition() {
return condition;
}
public void setCondition(Condition condition) {
this.condition = condition;
}
public int getRandomTemperature() {
return randomTemperature;
}
public void setRandomTemperature(int randomTemperature) {
this.randomTemperature = randomTemperature;
//更新数据
setChanged();
//将条件对象传递给各个观察者
notifyObservers(condition);
}
}
3定义观察者
/**
* @author lyf
* @projectName designPattern
* @date 2022/5/24 下午 06:24
* @description 异常温度观察者,当连续监控的温度次数超过临界值时,向配置的邮件地址发送警示邮箱
*/
public class AbnormalTemperatureObserver implements Observer {
//默认临界次数为0
private int qty=0;
@Override
public void update(Observable o, Object arg) {
TemperatureSubject subject=(TemperatureSubject)o;
Condition condition=(Condition)arg;
if(subject.getRandomTemperature()>=condition.getLimitTemperature()){
qty++;
saveToDataBase(subject);
}else{
//未达到临界时,连续超温记录被中断,将临界次数归零
qty=0;
saveToDataBase(subject);
}
if(qty>=condition.getLimitCount()){
sendMail(condition);
}
}
/**
* 记录温度
* @param subject
*/
public void saveToDataBase(TemperatureSubject subject){
//模拟记录温度数值
System.out.println("当前温度为"+subject.getRandomTemperature());
}
public void sendMail(Condition condition){
System.out.println("发送邮件"+condition.getMail());
}
}
/**
* @author lyf
* @projectName designPattern
* @date 2022/5/24 下午 06:20
* @description 正常温度观察者【仅仅记录数据】
*/
public class NormalTemperatureObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
TemperatureSubject subject=(TemperatureSubject)o;
Condition condition=(Condition)arg;
if(subject.getRandomTemperature()<condition.getLimitTemperature()){
//模拟记录温度数值
System.out.println("当前温度为"+subject.getRandomTemperature());
}
}
}
4 测试
public class Main {
private static final int RANDOM_SECONDS=2;
public static void main(String[] args) {
int[] array=new int []{30,35,29,33,31,34,35,32,30,25};
Condition condition=new Condition(30,5,"it-api@mail.foxconn.com");
TemperatureSubject sub=new TemperatureSubject();
//设置变量
sub.setCondition(condition);
NormalTemperatureObserver observer1=new NormalTemperatureObserver();
AbnormalTemperatureObserver observer2=new AbnormalTemperatureObserver();
//注册观察者
sub.addObserver(observer1);
sub.addObserver(observer2);
for(int i=0;i<array.length;i++){
int tempNum=array[i];
sub.setRandomTemperature(tempNum);
try {
//休眠两秒后继续观测变化的温度数据
TimeUnit.SECONDS.sleep(RANDOM_SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试结果如下
当前温度为30
当前温度为35
当前温度为29
当前温度为29
当前温度为33
当前温度为31
当前温度为34
当前温度为35
当前温度为32
发送邮件it-api@mail.foxconn.com
当前温度为30
当前温度为25
当前温度为25