1
0
Fork 0
mirror of https://github.com/tmrts/go-patterns.git synced 2025-04-11 21:10:55 +00:00
go-patterns/structural/bridge.md
2017-11-08 11:00:57 +08:00

1.1 KiB

Bridge Pattern

Decouples an interface from its implementation so that the two can vary independently

Implementation

type Info struct {
	Sender // who or which way to send information.        eg: QQ Wechat
	MsgTag // the tag of msg.  just like log level tag.    eg: COMMON IMPORTANCE
}

func (i Info) Sending(to, msg string) {
	msg = i.MsgTag(msg)
	i.Sender.Send(to, msg)
}

// interface

type Sender interface {
	Send(to, msg string)
}

type MsgTag func(string) string


// implementation

type QQSender struct {
}

func (QQSender) Send(to, msg string) {
	fmt.Println("[QQ] send to " + to + " with message: " + msg)
}

type WechatSender struct {
}

func (WechatSender) Send(to, msg string) {
	fmt.Println("[Wechat] send to " + to + " with message: " + msg)
}

func ImportanceTag(msg string) string {
	return "[IMPORTANCE] " + msg
}

func CommonTag(msg string) string {
	return "[COMMON] " + msg
}

Usage

info := &Info{MsgTag: ImportanceTag, Sender: QQSender{}}
info.Sending("cbping", "hello world")

info.Sender = WechatSender{}
info.Sending("cbping", "hello world")

info.MsgTag = CommonTag
info.Sending("cbping", "hello world")