go-pattern-examples/structure/03_bridge/bridge.go

80 lines
1.8 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package bridge
import "fmt"
2020-04-28 11:37:17 +03:00
//IMessage 发送消息接口
type IMessage interface {
NoticeUser(text string)
Priority() int
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//ISMSMessage send SMS MSG
type ISMSMessage interface {
//延迟接口的实现到其他类中
NoticeUser(text string, noticeMessage IMessage)
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//WSMessage MSG
type WSMessage struct {
Handler ISMSMessage //持有进一步实现的引用关系
Level int
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//NoticeUser by SMS
func (w *WSMessage) NoticeUser(text string) {
//转递消息给其他对象,相当于承上启下
fmt.Println("Websocket Notice User...", text)
//转递消息给其他对象,相当于承上启下,并且需要把上下文变量传递下去
if w.Handler != nil {
w.Handler.NoticeUser(text, w)
}
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//Priority of SMS
func (w *WSMessage) Priority() int {
return w.Level
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//EmailMessage MSG
type EmailMessage struct {
Handler ISMSMessage
Level int
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//NoticeUser by SMS
func (e *EmailMessage) NoticeUser(text string) {
//转递消息给其他对象,相当于承上启下,并且需要把上下文变量传递下去
fmt.Println("Email Notice User...", text)
if e.Handler != nil {
e.Handler.NoticeUser(text, e)
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//Priority of SMS
func (e *EmailMessage) Priority() int {
return e.Level
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
///需要实现具体的消息发送行为
//EmergencyWSMessage 紧急的短信消息
type EmergencyWSMessage struct {
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//NoticeUser by email
func (e *EmergencyWSMessage) NoticeUser(text string, noticeMessage IMessage) {
fmt.Println("Notice User", text, " By Websocket:", "with Level: ", noticeMessage.Priority())
2020-04-21 17:50:21 +03:00
}
2020-04-28 11:37:17 +03:00
//EmergencyEmailMessage 紧急的短信消息
type EmergencyEmailMessage struct {
}
//NoticeUser by email
func (e *EmergencyEmailMessage) NoticeUser(text string, noticeMessage IMessage) {
fmt.Println("Notice User:", text, " By Email:", "with Level: ", noticeMessage.Priority())
2020-04-21 17:50:21 +03:00
}