标签:一文 桥接 struct Notify func msg 搞懂 type string
原理
将抽象和实现解耦,让它们可以独立变化
这里的抽象不是指"接口"或者"抽象类",而是和具体实现无关的一套类库;这里的实现不是指"接口的实现类",而是和具体实现有关的一套类库
这里的说法稍微有些抽象,看下面的需求实现更好理解
需求实现
需求背景
根据不同的报警规则,进行不同类型的报警。
报警包含多种通知渠道:邮件、短信、微信、电话等。
通知紧急程度有多种类型:FATAL、URGENCY、NORMAL、TRIVIAL。
FATAL对应电话报警、URGENCY对应微信报警、NORMAL对应短信报警、TRIVIAL对应邮件报警
版本1实现
const (
FATAL = iota
URGENCY
NORMAL
TRIVIAL
)
type Notification struct{}
func (n *Notification) Notify(alertLevel int, msg string) {
if alertLevel == FATAL {
//电话报警
} else if alertLevel == URGENCY {
//微信报警
} else if alertLevel == NORMAL {
//短信报警
} else if alertLevel == TRIVIAL {
//邮件报警
}
}
type ApiInfo struct {
api string
errorCount int
totalCount int
}
type ErrAlertHandler struct {
notification *Notification
}
func (h *ErrAlertHandler) handle(apiInfo ApiInfo) {
if float64(apiInfo.errorCount)/float64(apiInfo.totalCount) > 0.5 {
h.notification.Notify(FATAL, "严重问题")
}
}
我们可以发现Notify方法中存在一堆if、else逻辑,如果if、else中的逻辑不复杂并且不存在无限扩展那么使用if、else没有问题,但是这里的不同渠道通知的逻辑都比较复杂并且还存在扩展的可能,那么Notify方法可能就会变得非常大,导致代码的可读性、可维护性下降
版本2改进
采用桥接模式,将 Notification这个抽象 和 具体的渠道通知实现 解耦,让它们可以独立开发,通过组合关系自由组合,这里的自由组合
是指将不同的告警级别和通知渠道组合(即将if条件和具体的实现组合)
我们将具体的渠道通知逻辑抽离出来单独实现,形成MsgSender消息发送类;然后将不同的紧急程度和不同的MsgSender组合形成不同的Notification
type MsgSender interface {
Send(msg string)
}
type TelephoneMsgSender struct {
TelephoneList []string
}
func (s *TelephoneMsgSender) Send(msg string) {
//电话通知
}
type WeChatMsgSender struct {
WeChatIDList []string
}
func (s *WeChatMsgSender) Send(msg string) {
//微信通知
}
type ShorMessageMsgSender struct {
TelephoneList []string
}
func (s *ShorMessageMsgSender) Send(msg string) {
//短信通知
}
type EmailMsgSender struct {
EmailList []string
}
func (s *EmailMsgSender) Send(msg string) {
//邮件通知
}
type AbstractNotification struct {
msgSender MsgSender
}
func (n *AbstractNotification) Notify(msg string) {
panic("abstract method")
}
type FatalNotification struct {
AbstractNotification
}
func (n *FatalNotification) Notify(msg string) {
n.msgSender.Send(msg)
}
type UrgencyNotification struct {
AbstractNotification
}
func (n *UrgencyNotification) Notify(msg string) {
n.msgSender.Send(msg)
}
type NormalNotification struct {
AbstractNotification
}
func (n *NormalNotification) Notify(msg string) {
n.msgSender.Send(msg)
}
type TrivialNotification struct {
AbstractNotification
}
func (n *TrivialNotification) Notify(msg string) {
n.msgSender.Send(msg)
}
func (h *ErrAlertHandler) handleV2(apiInfo ApiInfo) {
if float64(apiInfo.errorCount)/float64(apiInfo.totalCount) > 0.5 {
n := new(FatalNotification)
n.msgSender = new(TelephoneMsgSender)
n.Notify("严重问题")
}
}
这样新增新的渠道通知,只需要扩展一个MsgSender的实现类即可,做到开闭原则
标签:一文,桥接,struct,Notify,func,msg,搞懂,type,string 来源: https://blog.csdn.net/qq_36059306/article/details/119345838
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。