Bridge
程序员文章站
2022-07-16 16:09:55
...
一、前言
由于某些类型的固有的实现逻辑,使得它们具有两个变化的维度,乃至多个纬度的变化。桥接模式将抽象部分(业务功能)与实现部分(平台实现)分离,使它们都可以独立地变化,而不引入额外的复杂度。桥接模式类似于策略模式,区别在于策略模式封装一系列算法使得算法可以互相替换。策略模式使抽象部分和实现部分分离,可以独立变化。
二、类图
三、示例
1. 没有使用模式
package bridge
type Image struct {
}
type PCMessageBase struct{
};
func (this *PCMessageBase)PlaySound() {
}
func (this *PCMessageBase)DrawShape() {
}
func (this *PCMessageBase)WriteText() {
}
func (this *PCMessageBase)Connect() {
}
func NewPCMessageLite(pcMessageBase *PCMessageBase) *PCMessageLite {
return &PCMessageLite {
PCMessageBase:pcMessageBase,
}
}
type PCMessageLite struct {
*PCMessageBase
}
func (this *PCMessageLite)Login(username, password string) {
this.Connect()
}
func (this *PCMessageLite)SendMessage(message string) {
this.WriteText()
}
func (this *PCMessageLite)SendPicture(image Image) {
this.DrawShape()
}
func NewPCMessagePerfect(pcMessageBase *PCMessageBase) *PCMessagePerfect {
return &PCMessagePerfect {
PCMessageBase:pcMessageBase,
}
}
type PCMessagePerfect struct {
*PCMessageBase
}
func (this *PCMessagePerfect)Login(username, password string) {
this.PlaySound()
this.Connect()
}
func (this *PCMessagePerfect)SendMessage(message string) {
this.PlaySound()
this.WriteText()
}
func (this *PCMessagePerfect)SendPicture(image Image) {
this.PlaySound()
this.DrawShape()
}
type MobileMessageBase struct{
};
func (this *MobileMessageBase)PlaySound() {
}
func (this *MobileMessageBase)DrawShape() {
}
func (this *MobileMessageBase)WriteText() {
}
func (this *MobileMessageBase)Connect() {
}
func NewMobileMessageLite(mobileMessageBase *MobileMessageBase) *MobileMessageLite {
return &MobileMessageLite {
MobileMessageBase:mobileMessageBase,
}
}
type MobileMessageLite struct {
*MobileMessageBase
}
func (this *MobileMessageLite)Login(username, password string) {
this.Connect()
}
func (this *MobileMessageLite)SendMessage(message string) {
this.WriteText()
}
func (this *MobileMessageLite)SendPicture(image Image) {
this.DrawShape()
}
func NewMobileMessagePerfect(mobileMessageBase *MobileMessageBase) *MobileMessagePerfect {
return &MobileMessagePerfect {
MobileMessageBase:mobileMessageBase,
}
}
type MobileMessagePerfect struct {
*MobileMessageBase
}
func (this *MobileMessagePerfect)Login(username, password string) {
this.PlaySound()
this.Connect()
}
func (this *MobileMessagePerfect)SendMessage(message string) {
this.PlaySound()
this.WriteText()
}
func (this *MobileMessagePerfect)SendPicture(image Image) {
this.PlaySound()
this.DrawShape()
}
func Process() {
mb := &MobileMessageBase{}
mf := NewMobileMessagePerfect(mb)
mf.Connect()
}
2. 使用模式
package bridge
type Image struct {
}
type Message interface{
Login(username, password string)
SendMessage(message string)
SendPicture(image Image)
}
type MessageImp interface{
PlaySound()
DrawShape()
WriteText()
Connect()
}
type PCMessageBase struct{
};
func (this *PCMessageBase)PlaySound() {
}
func (this *PCMessageBase)DrawShape() {
}
func (this *PCMessageBase)WriteText() {
}
func (this *PCMessageBase)Connect() {
}
type MobileMessageBase struct{
};
func (this *MobileMessageBase)PlaySound() {
}
func (this *MobileMessageBase)DrawShape() {
}
func (this *MobileMessageBase)WriteText() {
}
func (this *MobileMessageBase)Connect() {
}
func NewMessageLite(messageImp MessageImp) *MessageLite{
return &MessageLite{
MessageImp: messageImp,
}
}
type MessageLite struct {
MessageImp
}
func (this *MessageLite)Login(username, password string) {
this.Connect()
}
func (this *MessageLite)SendMessage(message string) {
this.WriteText()
}
func (this *MessageLite)SendPicture(image Image) {
this.DrawShape()
}
func NewMessagePerfect(messageImp MessageImp) *MessagePerfect{
return &MessagePerfect{
MessageImp: messageImp,
}
}
type MessagePerfect struct {
MessageImp
}
func (this *MessagePerfect)Login(username, password string) {
this.PlaySound()
this.Connect()
}
func (this *MessagePerfect)SendMessage(message string) {
this.PlaySound()
this.WriteText()
}
func (this *MessagePerfect)SendPicture(image Image) {
this.PlaySound()
this.DrawShape()
}
func Process() {
pc := &PCMessageBase{}
ml := NewMessageLite(pc)
ml.Login("test", "test")
}
四、总结
- Bridge模式使用对象间的组合关系解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化,所谓抽象和实现沿着各自纬度的变化,即子类化它们。
- Bridge模式的应用一般在两个非常强的变化维度,有时一个 类也有多于两个的变化维度,这时可以使用Bridge的扩展模式。