Bridge mode

Bridge (Handle / Body)

1. Intention

Separate the abstract part from its realization part so that they can all be changed independently.

2. Applicability

The Bridge mode is used in the following situations:

  • You don't want to have a fixed binding relationship between abstraction and its implementation. For example, this situation may be because the implementation part should be selected or switched at the time the program is running.
  • The abstraction of the class and its implementation should be able to be expanded by generating subclasses. At this time, Bridge mode allows you to combine different abstract interfaces and implementation parts, and expand them separately.
  • The modification of an abstract implementation part should not affect the client, that is, the client's code does not need to be recompiled.
  • (C++) You want to completely hide the abstract implementation part from the client. In C++, the representation of the class is visible in the class interface.
  • As shown in the first class diagram in the Intent section, there are many classes to generate. Such a class hierarchy means that you must decompose an object into two parts. Rumbaugh calls this class hierarchy "nested generalizations".
  • You want to share the implementation between multiple objects (possibly using reference counting), but at the same time require that the client does not know this. A simple example is Coplien's String class, in which multiple objects can share the same string representation (StringRep).

3. Structure

Insert picture description here

4. Code

package bridge

/*
# 桥接模式
桥接模式分离抽象部分和实现部分。使得两部分独立扩展。
桥接模式类似于策略模式,区别在于策略模式封装一系列算法使得算法可以互相替换。
策略模式使抽象部分和实现部分分离,可以独立变化。

抽象类和它的实现构成了桥接的关系,使它们可以分别的变化。
礼物的类型可以随意变化,礼物也可以随便变化,但是无论如何变化,都不影响某种类型嵌入某种礼物


GG追MM,GG可以给MM送【不同种类】的【不同礼物】

*/

import (
	"fmt"
	"testing"
)

type GiftImpl interface {
    
    
	GiftName() string
}

type Book struct {
    
    }  //从GiftImpl继承

func (Book) GiftName() string {
    
    
	return "一本[%s]类型的[书]"
}

func NewBook() Book {
    
    
	return Book{
    
    }
}

type Flower struct {
    
    } // //从GiftImpl继承

func (Flower) GiftName() string {
    
    
	return "一朵[%s]类型的[花]"
}

func NewFlower() Flower {
    
    
	return Flower{
    
    }
}



type Gift interface {
    
    
	GiftType() string
}

type WarmGift struct {
    
    
	impl GiftImpl
}

func NewWarmGift(impl GiftImpl) WarmGift {
    
    
	return WarmGift{
    
    impl}
}


func (this WarmGift)GiftType() string {
    
    
	return fmt.Sprintf(this.impl.GiftName(),"温暖")
}

type WildGift struct {
    
    
	impl GiftImpl
}

func NewWildGift(impl GiftImpl) WildGift {
    
    
	return WildGift{
    
    impl}
}

func (this WildGift)GiftType() string {
    
    
	return fmt.Sprintf(this.impl.GiftName(),"狂野")
}


type MM struct {
    
    name string}
type GG struct {
    
    name string}

func (this GG)Chase(mm MM){
    
    
	var gift Gift

	gift = NewWarmGift(NewBook())
	this.Give(mm, gift)

	gift = NewWarmGift(NewFlower())
	this.Give(mm,gift)


	gift = NewWildGift(NewBook())
	this.Give(mm, gift)

	gift =NewWildGift(NewFlower())
	this.Give(mm,gift)
}

func (this GG)Give(mm MM,gift Gift)  {
    
    
	fmt.Println(this.name + "送给" + mm.name + gift.GiftType())
}

func TestGift(t *testing.T)  {
    
    

	gg:=GG{
    
    "[井上十三香]"}
	mm:=MM{
    
    "[川岛辣椒酱]"}
	gg.Chase(mm)

}




//---------------------------------------

















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)
}

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
}







// IMsgSender IMsgSender
type IMsgSender interface {
    
      //消息发送接口
	Send(msg string) error
}

// EmailMsgSender 发送邮件
// 可能还有 电话、短信等各种实现
type EmailMsgSender struct {
    
    
	emails []string
}

// NewEmailMsgSender NewEmailMsgSender
func NewEmailMsgSender(emails []string) *EmailMsgSender {
    
    
	return &EmailMsgSender{
    
    emails: emails}
}

// Send Send
func (s *EmailMsgSender) Send(msg string) error {
    
     //实现接口
	fmt.Println("发送邮件",msg)
	// 这里去发送消息
	return nil
}


// INotification 通知接口
type INotification interface {
    
    
	Notify(msg string) error
}

// ErrorNotification 错误通知
// 后面可能还有 warning 各种级别
type ErrorNotification struct {
    
     //
	sender IMsgSender
}

// NewErrorNotification NewErrorNotification
func NewErrorNotification(sender IMsgSender) *ErrorNotification {
    
    
	return &ErrorNotification{
    
    sender: sender}
}

// Notify 发送通知
func (n *ErrorNotification) Notify(msg string) error {
    
    
	return n.sender.Send(msg)
}


func TestErrorNotification_Notify(t *testing.T) {
    
    
	sender := NewEmailMsgSender([]string{
    
    "[email protected]"})
	n := NewErrorNotification(sender)
	err := n.Notify("test msg")

	t.Log(err)
}

Guess you like

Origin blog.csdn.net/dawnto/article/details/112973759