关于java:桥接模式的示例

依据不同的告警规定,触发不同类型的告警。告警反对多种告诉渠道,包含:邮件、短信、微信、主动语音电话。告诉的紧急水平有多种类型,包含:SEVERE(重大)、URGENCY(紧急)、NORMAL(一般)、TRIVIAL(无关紧要)。不同的紧急水平对应不同的告诉渠道。比方,SERVE(重大)级别的音讯会通过“主动语音电话”告知相干人员。


import java.util.List;

public enum NotificationEmergencyLevel {
    SEVERE, URGENCY, NORMAL, TRIVIAL
}

class Notification{
    private List emailAddresses;
    private List telephones;
    private List wechatIds;
    public Notification() {}

    public void setEmailAddress(List emailAddress) {
        this.emailAddresses = emailAddress;
    }
    public void setTelephones(List telephones) {
        this.telephones = telephones;
    }
    public void setWechatIds(List wechatIds) {
        this.wechatIds = wechatIds;
    }

    public void notify(NotificationEmergencyLevel level,String message){
        if (level.equals(NotificationEmergencyLevel.SEVERE)){
            //电话告诉
        }else if (level.equals(NotificationEmergencyLevel.URGENCY)){
            //微信
        }else  if (level.equals(NotificationEmergencyLevel.NORMAL)){
            //邮件
        }else if (level.equals(NotificationEmergencyLevel.TRIVIAL)){
            //邮件
        }
    }
}


//客户端如何应用
class ErrorAlertHandler extends AlertHandler {
    public ErrorAlertHandler(AlertRule rule, Notification notification){
        super(rule, notification);
    }
    @Override
    public void check(ApiStatInfo apiStatInfo){
        if (apiStatInfo.getErrorCount() > rule.getMatchedRule(apiStatInfo.getApi()).getMaxErrorCount()){
            notification.notify(NotificationEmergencyLevel.SEVERE, "...");
        }
    }
}

/*Notification类有一处显著的问题就是应用的if太多了,另外if里的是发送报警逻辑会很简单,
咱们将不同渠道的发送逻辑剥离进去,造成独立的音讯发送类(MsgSender 相干类)。
其中,Notification 类相当于形象,MsgSender 类相当于实现,两者能够独立开发,通过组合关系(也就是桥梁)任意组合在一起*/

---------------改良版本-------------

interface MsgSender{
    void send(String message);
}

class TelephoneMsgSender implements MsgSender{
    private List telephones;
    public TelephoneMsgSender(List telephones) {
        this.telephones = telephones;
    }
    @Override
    public void send(String message) {
        //...
    }
}
class EmailMsgSender implements MsgSender {
    // 与TelephoneMsgSender代码构造相似,所以省略...
}
class WechatMsgSender implements MsgSender {
    // 与TelephoneMsgSender代码构造相似,所以省略...
}

abstract class Notification {
    protected MsgSender msgSender;

    public Notification(MsgSender msgSender) {
        this.msgSender = msgSender;
    }

    public abstract void notify(String message);
}

class SevereNotification extends Notification {
    public SevereNotification(MsgSender msgSender){
        super(msgSender);
    }

    @Override
    public void notify(String message) {
        msgSender.send(message);
    }
}

class UrgencyNotification extends Notification {// 与SevereNotification代码构造相似,所以省略...}
    
class NormalNotification extends Notification { // 与SevereNotification代码构造相似,所以省略...}

class TrivialNotification extends Notification { // 与SevereNotification代码构造相似,所以省略...}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理