DesignPatternBridge桥接模式

DesignPatternBridge桥接模式

桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。

意图: 将抽象部分与实现部分分离,使它们都可以独立的变化

管方代码

package bridge

import "fmt"

type AbstractMessage interface {
	SendMessage(text, to string)
}

type MessageImplementer interface {
	Send(text, to string)
}

type MessageSMS struct{}

func ViaSMS() MessageImplementer {
	return &MessageSMS{}
}

func (*MessageSMS) Send(text, to string) {
	fmt.Printf("send %s to %s via SMS", text, to)
}

type MessageEmail struct{}

func ViaEmail() MessageImplementer {
	return &MessageEmail{}
}

func (*MessageEmail) Send(text, to string) {
	fmt.Printf("send %s to %s via Email", text, to)
}

type CommonMessage struct {
	method MessageImplementer
}

func NewCommonMessage(method MessageImplementer) *CommonMessage {
	return &CommonMessage{
		method: method,
	}
}

func (m *CommonMessage) SendMessage(text, to string) {
	m.method.Send(text, to)
}

type UrgencyMessage struct {
	method MessageImplementer
}

func NewUrgencyMessage(method MessageImplementer) *UrgencyMessage {
	return &UrgencyMessage{
		method: method,
	}
}

func (m *UrgencyMessage) SendMessage(text, to string) {
	m.method.Send(fmt.Sprintf("[Urgency] %s", text), to)
}

测试代码

package bridge

func ExampleCommonSMS() {
	m := NewCommonMessage(ViaSMS())
	m.SendMessage("have a drink?", "bob")
	// Output:
	// send have a drink? to bob via SMS
}

func ExampleCommonEmail() {
	m := NewCommonMessage(ViaEmail())
	m.SendMessage("have a drink?", "bob")
	// Output:
	// send have a drink? to bob via Email
}

func ExampleUrgencySMS() {
	m := NewUrgencyMessage(ViaSMS())
	m.SendMessage("have a drink?", "bob")
	// Output:
	// send [Urgency] have a drink? to bob via SMS
}

func ExampleUrgencyEmail() {
	m := NewUrgencyMessage(ViaEmail())
	m.SendMessage("have a drink?", "bob")
	// Output:
	// send [Urgency] have a drink? to bob via Email
}

毛哥通俗讲解

  • 突然想学画画,想用红色的笔画个压缩
  • 学习一段时间之后,对于自己的审美有严格要求了
  • 这个笔有大号小号之分,颜色也有了色彩差别

UML图

代码实现

package my

import "fmt"

// 抽象
type Pen interface {
   SetColor(color Color)
   Draw(what string)
}

// 扩充抽象类
type BigPen struct {
   PenType string
   Color Color
}

func NewBigPen() *BigPen {
   return &BigPen{PenType:"big_pen"}
}

func (bp *BigPen) SetColor(c Color) {
   bp.Color = c
}

func (bp *BigPen) Draw(what string) {
   bp.Color.BePaint(bp.PenType, what)
}

type SmallPen struct {
   Color Color
   PenType string
}

func NewSmallPen() *SmallPen {
   return &SmallPen{PenType:"small_pen"}
}

func (sp *SmallPen) SetColor(c Color) {
   sp.Color = c
}

func (sp *SmallPen) Draw(what string) {
   sp.Color.BePaint(sp.PenType, what)
}

// 实现类
type Color interface {
   BePaint(penType string, what string)
}

// 红色
type Red struct {
   ColorType string
}

func NewRed() *Red {
   return &Red{ColorType:"红色"}
}

func (r *Red) BePaint(penType string, what string) {
   fmt.Println(fmt.Sprintf("型号笔->%s, 颜色-> %s,实物-> %s", penType, r.ColorType, what))
}

type Green struct {
   ColorType string
}

func NewGreen() *Green {
   return &Green{ColorType:"绿色"}
}

func (g *Green) BePaint(penType string, what string) {
   fmt.Println(fmt.Sprintf("型号笔->%s, 颜色-> %s,实物-> %s", penType, g.ColorType, what))
}

func TTmain() {
   // 红色的大好笔画亚索
   bp := NewBigPen()
   bp.SetColor(NewRed())
   bp.Draw("亚索")

   // 绿色的小号笔画盲仔
   sp := NewSmallPen()
   sp.SetColor(NewGreen())
   sp.Draw("盲仔")
}


# 通过ttmain测试输出
型号笔->big_pen, 颜色-> 红色,实物-> 亚索
型号笔->small_pen, 颜色-> 绿色,实物-> 盲仔

写法要点

最后具体实现落在了bepaint()

posted @ 2020-12-11 09:58  maob  阅读(70)  评论(0编辑  收藏  举报