创建文件Message.go
package bridge // SNS短信 // Email type AbstractMessage interface { SendMessage(text, to string) // 发送快,普通发送 } type MessageImlementer interface { Send(text, to string) // 短信,邮件 }
创建文件MessageEmail.go
package bridge import "fmt" type MessageEmail struct { } func ViaEmail() MessageImlementer { return &MessageEmail{} } func (ms *MessageEmail) Send(text, to string) { // 邮件发送 fmt.Printf("send %s to %s via Email\n", text, to) }
创建文件MessageSMS.go
package bridge import "fmt" type MessageSMS struct { } func ViaSMS() MessageImlementer { return &MessageSMS{} } func (ms *MessageSMS) Send(text, to string) { // 短信发送 fmt.Printf("send %s to %s via SMS\n", text, to) }
创建文件UrgencyMessage.go
package bridge import "fmt" type UrgencyMessage struct { method MessageImlementer } func NewUrgencyMessage(method MessageImlementer) *UrgencyMessage { return &UrgencyMessage{method: method} } func (m *UrgencyMessage) SendMessage(text, to string) { m.method.Send(fmt.Sprintf("发送到[%s]", text), to) // 很快速度发送 }
main.go
package main import "ssp_api_go/test/design/bridge" func main() { m := bridge.NewUrgencyMessage(bridge.ViaEmail()) m.SendMessage("你好", "Jackey") }