大话设计模式之单例,策略,简单工厂

Posted on 2023-06-03 16:31  嘿!!!!  阅读(10)  评论(0编辑  收藏  举报

基于实际面试题实现

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 用java设计一个机房环境监测系统的代码框架,机房有多种传感器,可以实时检测机房的温度、湿度、噪音等数据,
 * 并把数据实时发送到监控中心,其中某项数据超过不健康阈值立即上报到监控中心显示
 * 要求:
 * 1、程序可以检测并展示机房温度、湿度、噪音等数据。
 * 2、程序至少使用一种设计模式
 * 3、程序需要考虑可扩展性和易维护性
 * 4、有良好的编程规范
 * 5、不要依赖除jdk以外的库
 */

public class Demo {
    public static void main(String[] args) {

        while (true) {
            //传感器实时监听
            Room room = new Room("机房1");
            monitoringCenter(room);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static void monitoringCenter(Room room) {
        //发送道监控中心
        room.getSensorMap().forEach((sensorEeum, integer) -> {
            SensorStrategyFactory sensorStrategyFactory = SensorStrategyFactory.getFactory();
            SensorStrategy sensorStrategy = sensorStrategyFactory.getSensorStrategy(sensorEeum);
            boolean unHealthy = sensorStrategy.unHealthy(integer);
            if (unHealthy) {
                //超过不健康阈值立即上报到监控中心显示
                System.out.println(room.getRoomName() + "--" + sensorEeum.getErrMsg());
            }
        });
    }
}


class Room {
    /**
     * 传感器组合
     */
    private Map<SensorEeum, Integer> sensorMap = new HashMap<>();
    /**
     * 房间名称
     */
    private String roomName;

    /**
     * 实时监控
     */
    public Room(String roomName) {
        Map<SensorEeum, Integer> map = new HashMap<>();
        map.put(SensorEeum.TEMPERATURE, new Random().nextInt(100));
        map.put(SensorEeum.WET, new Random().nextInt(200));
        map.put(SensorEeum.NOISE, new Random().nextInt(400));
        this.sensorMap = map;
        this.roomName = roomName;
    }

    public Map<SensorEeum, Integer> getSensorMap() {
        return sensorMap;
    }

    public void setSensorMap(Map<SensorEeum, Integer> sensorMap) {
        this.sensorMap = sensorMap;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }
}

/**
 * 策略工厂(简单工厂模式,单例模式)
 */
class SensorStrategyFactory {
    private static final Map<SensorEeum, SensorStrategy> sensorStrategyMap = new HashMap<>();

    private static SensorStrategyFactory factory = null;

    static {
        SensorStrategy temperatureSensorStrategy = new TemperatureSensorStrategy();
        sensorStrategyMap.put(temperatureSensorStrategy.getSensorType(), temperatureSensorStrategy);
        SensorStrategy wetSensorStrategy = new WetSensorStrategy();
        sensorStrategyMap.put(wetSensorStrategy.getSensorType(), wetSensorStrategy);
        SensorStrategy noiseSensorStrategy = new NoiseSensorStrategy();
        sensorStrategyMap.put(noiseSensorStrategy.getSensorType(), noiseSensorStrategy);
    }

    private SensorStrategyFactory() {
    }

    public static SensorStrategyFactory getFactory() {
        if (factory == null) {

            //双重锁定
            synchronized (SensorStrategyFactory.class) {
                if (factory == null) {
                    factory = new SensorStrategyFactory();
                }
            }
        }
        return factory;
    }

    public SensorStrategy getSensorStrategy(SensorEeum sensorEeum) {
        if (sensorStrategyMap.containsKey(sensorEeum)) {
            return sensorStrategyMap.get(sensorEeum);
        }
        return null;
    }
}

/**
 * 传感器处理策略(策略模式)
 */
interface SensorStrategy {
    SensorEeum getSensorType();

    boolean unHealthy(int value);
}

abstract class abstractSensorStrategy implements SensorStrategy {
    @Override
    public boolean unHealthy(int value) {
        if (value > getSensorType().getSensorThreshold()) {
            return true;
        }
        return false;
    }
}

/**
 * 温度处理策略
 */
class TemperatureSensorStrategy extends abstractSensorStrategy {

    @Override
    public SensorEeum getSensorType() {
        return SensorEeum.TEMPERATURE;
    }


}

/**
 * 湿度处理策略
 */
class WetSensorStrategy extends abstractSensorStrategy {
    @Override
    public SensorEeum getSensorType() {
        return SensorEeum.WET;
    }

}

/**
 * 噪音处理策略
 */
class NoiseSensorStrategy extends abstractSensorStrategy {
    @Override
    public SensorEeum getSensorType() {
        return SensorEeum.NOISE;
    }

    @Override
    public boolean unHealthy(int value) {
        if (value > getSensorType().getSensorThreshold()) {
            return true;
        }
        return false;
    }
}

/**
 * 处理器枚举
 */
enum SensorEeum {
    TEMPERATURE("temperature", 50, "温度异常,请排查机房"),
    WET("wet", 100, "湿度异常,请排查机房"),
    NOISE("noise", 200, "噪音异常,请排查机房"),
    ;

    SensorEeum(String sensorType, int sensorThreshold, String errMsg) {
        this.sensorType = sensorType;
        this.sensorThreshold = sensorThreshold;
        this.errMsg = errMsg;
    }

    private String sensorType;
    private int sensorThreshold;
    private String errMsg;

    public String getSensorType() {
        return sensorType;
    }

    public int getSensorThreshold() {
        return sensorThreshold;
    }

    public String getErrMsg() {
        return errMsg;
    }
}